#37 reduce calls to resizeToScreen
[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) {
027de5ae 693 readBufferUTF8 = new char[2048];
d36057df 694 } else {
027de5ae 695 readBuffer = new byte[2048];
d36057df
KL
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() {
027de5ae 3461 Cell newCell = new Cell('E');
34a42e78
KL
3462 for (DisplayLine line: display) {
3463 for (int i = 0; i < line.length(); i++) {
3464 line.replace(i, newCell);
3465 }
3466 }
3467 }
3468
3469 /**
3470 * DECSCL - Compatibility level.
3471 */
3472 private void decscl() {
3473 int i = getCsiParam(0, 0);
3474 int j = getCsiParam(1, 0);
3475
3476 if (i == 61) {
3477 // Reset fonts
3478 currentState.g0Charset = CharacterSet.US;
3479 currentState.g1Charset = CharacterSet.DRAWING;
3480 s8c1t = false;
3481 } else if (i == 62) {
3482
3483 if ((j == 0) || (j == 2)) {
3484 s8c1t = true;
3485 } else if (j == 1) {
3486 s8c1t = false;
3487 }
3488 }
3489 }
3490
3491 /**
3492 * CUD - Cursor down.
3493 */
3494 private void cud() {
3495 cursorDown(getCsiParam(0, 1, 1, height), true);
3496 }
3497
3498 /**
3499 * CUF - Cursor forward.
3500 */
3501 private void cuf() {
3502 cursorRight(getCsiParam(0, 1, 1, rightMargin + 1), true);
3503 }
3504
3505 /**
3506 * CUB - Cursor backward.
3507 */
3508 private void cub() {
3509 cursorLeft(getCsiParam(0, 1, 1, currentState.cursorX + 1), true);
3510 }
3511
3512 /**
3513 * CUU - Cursor up.
3514 */
3515 private void cuu() {
3516 cursorUp(getCsiParam(0, 1, 1, currentState.cursorY + 1), true);
3517 }
3518
3519 /**
3520 * CUP - Cursor position.
3521 */
3522 private void cup() {
3523 cursorPosition(getCsiParam(0, 1, 1, height) - 1,
3524 getCsiParam(1, 1, 1, rightMargin + 1) - 1);
3525 }
3526
3527 /**
3528 * CNL - Cursor down and to column 1.
3529 */
3530 private void cnl() {
3531 cursorDown(getCsiParam(0, 1, 1, height), true);
3532 // To column 0
3533 cursorLeft(currentState.cursorX, true);
3534 }
3535
3536 /**
3537 * CPL - Cursor up and to column 1.
3538 */
3539 private void cpl() {
3540 cursorUp(getCsiParam(0, 1, 1, currentState.cursorY + 1), true);
3541 // To column 0
3542 cursorLeft(currentState.cursorX, true);
3543 }
3544
3545 /**
3546 * CHA - Cursor to column # in current row.
3547 */
3548 private void cha() {
3549 cursorPosition(currentState.cursorY,
3550 getCsiParam(0, 1, 1, rightMargin + 1) - 1);
3551 }
3552
3553 /**
3554 * VPA - Cursor to row #, same column.
3555 */
3556 private void vpa() {
3557 cursorPosition(getCsiParam(0, 1, 1, height) - 1,
3558 currentState.cursorX);
3559 }
3560
3561 /**
3562 * ED - Erase in display.
3563 */
3564 private void ed() {
3565 boolean honorProtected = false;
3566 boolean decPrivateModeFlag = false;
3567
bd8d51fa
KL
3568 for (int i = 0; i < collectBuffer.length(); i++) {
3569 if (collectBuffer.charAt(i) == '?') {
34a42e78 3570 decPrivateModeFlag = true;
bd8d51fa 3571 break;
34a42e78
KL
3572 }
3573 }
3574
3575 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3576 && (decPrivateModeFlag == true)
3577 ) {
3578 honorProtected = true;
3579 }
3580
3581 int i = getCsiParam(0, 0);
3582
3583 if (i == 0) {
3584 // Erase from here to end of screen
3585 if (currentState.cursorY < height - 1) {
3586 eraseScreen(currentState.cursorY + 1, 0, height - 1, width - 1,
3587 honorProtected);
3588 }
3589 eraseLine(currentState.cursorX, width - 1, honorProtected);
3590 } else if (i == 1) {
3591 // Erase from beginning of screen to here
3592 eraseScreen(0, 0, currentState.cursorY - 1, width - 1,
3593 honorProtected);
3594 eraseLine(0, currentState.cursorX, honorProtected);
3595 } else if (i == 2) {
3596 // Erase entire screen
3597 eraseScreen(0, 0, height - 1, width - 1, honorProtected);
3598 }
3599 }
3600
3601 /**
3602 * EL - Erase in line.
3603 */
3604 private void el() {
3605 boolean honorProtected = false;
3606 boolean decPrivateModeFlag = false;
3607
bd8d51fa
KL
3608 for (int i = 0; i < collectBuffer.length(); i++) {
3609 if (collectBuffer.charAt(i) == '?') {
34a42e78 3610 decPrivateModeFlag = true;
bd8d51fa 3611 break;
34a42e78
KL
3612 }
3613 }
3614
3615 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3616 && (decPrivateModeFlag == true)
3617 ) {
3618 honorProtected = true;
3619 }
3620
3621 int i = getCsiParam(0, 0);
3622
3623 if (i == 0) {
3624 // Erase from here to end of line
3625 eraseLine(currentState.cursorX, width - 1, honorProtected);
3626 } else if (i == 1) {
3627 // Erase from beginning of line to here
3628 eraseLine(0, currentState.cursorX, honorProtected);
3629 } else if (i == 2) {
3630 // Erase entire line
3631 eraseLine(0, width - 1, honorProtected);
3632 }
3633 }
3634
3635 /**
3636 * ECH - Erase # of characters in current row.
3637 */
3638 private void ech() {
3639 int i = getCsiParam(0, 1, 1, width);
3640
3641 // Erase from here to i characters
3642 eraseLine(currentState.cursorX, currentState.cursorX + i - 1, false);
3643 }
3644
3645 /**
3646 * IL - Insert line.
3647 */
3648 private void il() {
3649 int i = getCsiParam(0, 1);
3650
3651 if ((currentState.cursorY >= scrollRegionTop)
3652 && (currentState.cursorY <= scrollRegionBottom)
3653 ) {
3654
3655 // I can get the same effect with a scroll-down
3656 scrollingRegionScrollDown(currentState.cursorY,
3657 scrollRegionBottom, i);
3658 }
3659 }
3660
3661 /**
3662 * DCH - Delete char.
3663 */
3664 private void dch() {
3665 int n = getCsiParam(0, 1);
3666 DisplayLine line = display.get(currentState.cursorY);
3667 Cell blank = new Cell();
3668 for (int i = 0; i < n; i++) {
3669 line.delete(currentState.cursorX, blank);
3670 }
3671 }
3672
3673 /**
3674 * ICH - Insert blank char at cursor.
3675 */
3676 private void ich() {
3677 int n = getCsiParam(0, 1);
3678 DisplayLine line = display.get(currentState.cursorY);
3679 Cell blank = new Cell();
3680 for (int i = 0; i < n; i++) {
3681 line.insert(currentState.cursorX, blank);
3682 }
3683 }
3684
3685 /**
3686 * DL - Delete line.
3687 */
3688 private void dl() {
3689 int i = getCsiParam(0, 1);
3690
3691 if ((currentState.cursorY >= scrollRegionTop)
3692 && (currentState.cursorY <= scrollRegionBottom)) {
3693
3694 // I can get the same effect with a scroll-down
3695 scrollingRegionScrollUp(currentState.cursorY,
3696 scrollRegionBottom, i);
3697 }
3698 }
3699
3700 /**
3701 * HVP - Horizontal and vertical position.
3702 */
3703 private void hvp() {
3704 cup();
3705 }
3706
3707 /**
3708 * REP - Repeat character.
3709 */
3710 private void rep() {
3711 int n = getCsiParam(0, 1);
3712 for (int i = 0; i < n; i++) {
3713 printCharacter(repCh);
3714 }
3715 }
3716
3717 /**
3718 * SU - Scroll up.
3719 */
3720 private void su() {
3721 scrollingRegionScrollUp(scrollRegionTop, scrollRegionBottom,
3722 getCsiParam(0, 1, 1, height));
3723 }
3724
3725 /**
3726 * SD - Scroll down.
3727 */
3728 private void sd() {
3729 scrollingRegionScrollDown(scrollRegionTop, scrollRegionBottom,
3730 getCsiParam(0, 1, 1, height));
3731 }
3732
3733 /**
3734 * CBT - Go back X tab stops.
3735 */
3736 private void cbt() {
3737 int tabsToMove = getCsiParam(0, 1);
3738 int tabI;
3739
3740 for (int i = 0; i < tabsToMove; i++) {
3741 int j = currentState.cursorX;
3742 for (tabI = 0; tabI < tabStops.size(); tabI++) {
3743 if (tabStops.get(tabI) >= currentState.cursorX) {
3744 break;
3745 }
3746 }
3747 tabI--;
3748 if (tabI <= 0) {
3749 j = 0;
3750 } else {
3751 j = tabStops.get(tabI);
3752 }
3753 cursorPosition(currentState.cursorY, j);
3754 }
3755 }
3756
3757 /**
3758 * CHT - Advance X tab stops.
3759 */
3760 private void cht() {
3761 int n = getCsiParam(0, 1);
3762 for (int i = 0; i < n; i++) {
3763 advanceToNextTabStop();
3764 }
3765 }
3766
3767 /**
3768 * SGR - Select graphics rendition.
3769 */
3770 private void sgr() {
3771
3772 if (csiParams.size() == 0) {
3773 currentState.attr.reset();
3774 return;
3775 }
3776
051e2913
KL
3777 int sgrColorMode = -1;
3778 boolean idx88Color = false;
3779 boolean rgbColor = false;
3780 int rgbRed = -1;
3781 int rgbGreen = -1;
3782
34a42e78
KL
3783 for (Integer i: csiParams) {
3784
051e2913
KL
3785 if ((sgrColorMode == 38) || (sgrColorMode == 48)) {
3786
3787 assert (type == DeviceType.XTERM);
3788
3789 if (idx88Color) {
3790 /*
3791 * Indexed color mode, we now have the index number.
3792 */
3793 if (sgrColorMode == 38) {
3794 currentState.attr.setForeColorRGB(get88Color(i));
3795 } else {
3796 assert (sgrColorMode == 48);
3797 currentState.attr.setBackColorRGB(get88Color(i));
3798 }
3799 sgrColorMode = -1;
3800 idx88Color = false;
3801 continue;
3802 }
3803
3804 if (rgbColor) {
3805 /*
3806 * RGB color mode, we are collecting tokens.
3807 */
3808 if (rgbRed == -1) {
3809 rgbRed = i & 0xFF;
3810 } else if (rgbGreen == -1) {
3811 rgbGreen = i & 0xFF;
3812 } else {
3813 int rgb = rgbRed << 16;
3814 rgb |= rgbGreen << 8;
3815 rgb |= i & 0xFF;
3816
3817 // System.err.printf("RGB: %08x\n", rgb);
3818
3819 if (sgrColorMode == 38) {
3820 currentState.attr.setForeColorRGB(rgb);
3821 } else {
3822 assert (sgrColorMode == 48);
3823 currentState.attr.setBackColorRGB(rgb);
3824 }
3825 rgbRed = -1;
3826 rgbGreen = -1;
3827 sgrColorMode = -1;
3828 rgbColor = false;
3829 }
3830 continue;
3831 }
3832
3833 switch (i) {
3834
3835 case 2:
3836 /*
3837 * RGB color mode.
3838 */
3839 rgbColor = true;
3840 break;
3841
3842 case 5:
3843 /*
3844 * Indexed color mode.
3845 */
3846 idx88Color = true;
3847 break;
3848
3849 default:
3850 /*
3851 * This is neither indexed nor RGB color. Bail out.
3852 */
3853 return;
3854 }
3855
3856 } // if ((sgrColorMode == 38) || (sgrColorMode == 48))
3857
34a42e78
KL
3858 switch (i) {
3859
3860 case 0:
3861 // Normal
3862 currentState.attr.reset();
3863 break;
3864
3865 case 1:
3866 // Bold
3867 currentState.attr.setBold(true);
3868 break;
3869
3870 case 4:
3871 // Underline
3872 currentState.attr.setUnderline(true);
3873 break;
3874
3875 case 5:
3876 // Blink
3877 currentState.attr.setBlink(true);
3878 break;
3879
3880 case 7:
3881 // Reverse
3882 currentState.attr.setReverse(true);
3883 break;
3884
3885 default:
3886 break;
3887 }
3888
3889 if (type == DeviceType.XTERM) {
3890
3891 switch (i) {
3892
3893 case 8:
3894 // Invisible
3895 // TODO
3896 break;
3897
051e2913
KL
3898 case 90:
3899 // Set black foreground
3900 currentState.attr.setForeColorRGB(get88Color(8));
3901 break;
3902 case 91:
3903 // Set red foreground
3904 currentState.attr.setForeColorRGB(get88Color(9));
3905 break;
3906 case 92:
3907 // Set green foreground
3908 currentState.attr.setForeColorRGB(get88Color(10));
3909 break;
3910 case 93:
3911 // Set yellow foreground
3912 currentState.attr.setForeColorRGB(get88Color(11));
3913 break;
3914 case 94:
3915 // Set blue foreground
3916 currentState.attr.setForeColorRGB(get88Color(12));
3917 break;
3918 case 95:
3919 // Set magenta foreground
3920 currentState.attr.setForeColorRGB(get88Color(13));
3921 break;
3922 case 96:
3923 // Set cyan foreground
3924 currentState.attr.setForeColorRGB(get88Color(14));
3925 break;
3926 case 97:
3927 // Set white foreground
3928 currentState.attr.setForeColorRGB(get88Color(15));
3929 break;
3930
3931 case 100:
3932 // Set black background
3933 currentState.attr.setBackColorRGB(get88Color(8));
3934 break;
3935 case 101:
3936 // Set red background
3937 currentState.attr.setBackColorRGB(get88Color(9));
3938 break;
3939 case 102:
3940 // Set green background
3941 currentState.attr.setBackColorRGB(get88Color(10));
3942 break;
3943 case 103:
3944 // Set yellow background
3945 currentState.attr.setBackColorRGB(get88Color(11));
3946 break;
3947 case 104:
3948 // Set blue background
3949 currentState.attr.setBackColorRGB(get88Color(12));
3950 break;
3951 case 105:
3952 // Set magenta background
3953 currentState.attr.setBackColorRGB(get88Color(13));
3954 break;
3955 case 106:
3956 // Set cyan background
3957 currentState.attr.setBackColorRGB(get88Color(14));
3958 break;
3959 case 107:
3960 // Set white background
3961 currentState.attr.setBackColorRGB(get88Color(15));
3962 break;
3963
34a42e78
KL
3964 default:
3965 break;
3966 }
3967 }
3968
3969 if ((type == DeviceType.VT220)
3970 || (type == DeviceType.XTERM)) {
3971
3972 switch (i) {
3973
3974 case 22:
3975 // Normal intensity
3976 currentState.attr.setBold(false);
3977 break;
3978
3979 case 24:
3980 // No underline
3981 currentState.attr.setUnderline(false);
3982 break;
3983
3984 case 25:
3985 // No blink
3986 currentState.attr.setBlink(false);
3987 break;
3988
3989 case 27:
3990 // Un-reverse
3991 currentState.attr.setReverse(false);
3992 break;
3993
3994 default:
3995 break;
3996 }
3997 }
3998
3999 // A true VT100/102/220 does not support color, however everyone
4000 // is used to their terminal emulator supporting color so we will
4001 // unconditionally support color for all DeviceType's.
4002
4003 switch (i) {
4004
4005 case 30:
4006 // Set black foreground
4007 currentState.attr.setForeColor(Color.BLACK);
051e2913 4008 currentState.attr.setForeColorRGB(-1);
34a42e78
KL
4009 break;
4010 case 31:
4011 // Set red foreground
4012 currentState.attr.setForeColor(Color.RED);
051e2913 4013 currentState.attr.setForeColorRGB(-1);
34a42e78
KL
4014 break;
4015 case 32:
4016 // Set green foreground
4017 currentState.attr.setForeColor(Color.GREEN);
051e2913 4018 currentState.attr.setForeColorRGB(-1);
34a42e78
KL
4019 break;
4020 case 33:
4021 // Set yellow foreground
4022 currentState.attr.setForeColor(Color.YELLOW);
051e2913 4023 currentState.attr.setForeColorRGB(-1);
34a42e78
KL
4024 break;
4025 case 34:
4026 // Set blue foreground
4027 currentState.attr.setForeColor(Color.BLUE);
051e2913 4028 currentState.attr.setForeColorRGB(-1);
34a42e78
KL
4029 break;
4030 case 35:
4031 // Set magenta foreground
4032 currentState.attr.setForeColor(Color.MAGENTA);
051e2913 4033 currentState.attr.setForeColorRGB(-1);
34a42e78
KL
4034 break;
4035 case 36:
4036 // Set cyan foreground
4037 currentState.attr.setForeColor(Color.CYAN);
051e2913 4038 currentState.attr.setForeColorRGB(-1);
34a42e78
KL
4039 break;
4040 case 37:
4041 // Set white foreground
4042 currentState.attr.setForeColor(Color.WHITE);
051e2913 4043 currentState.attr.setForeColorRGB(-1);
34a42e78
KL
4044 break;
4045 case 38:
14c78e1b
KL
4046 if (type == DeviceType.XTERM) {
4047 /*
4048 * Xterm supports T.416 / ISO-8613-3 codes to select
4049 * either an indexed color or an RGB value. (It also
4050 * permits these ISO-8613-3 SGR sequences to be separated
4051 * by colons rather than semicolons.)
4052 *
051e2913
KL
4053 * We will support only the following:
4054 *
4055 * 1. Indexed color mode (88- or 256-color modes).
14c78e1b 4056 *
051e2913 4057 * 2. Direct RGB.
14c78e1b 4058 *
051e2913
KL
4059 * These cover most of the use cases in the real world.
4060 *
4061 * HOWEVER, note that this is an awful broken "standard",
4062 * with no way to do it "right". See
4063 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4064 * for a detailed discussion of the current state of RGB
4065 * in various terminals, the point of which is that none
4066 * of them really do the same thing despite all appearing
4067 * to be "xterm".
4068 *
4069 * Also see
4070 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4071 * where it is assumed that supporting just the "indexed
4072 * mode" of these sequences (which could align easily
4073 * with existing SGR colors) is assumed to mean full
4074 * support of 24-bit RGB. So it is all or nothing.
14c78e1b
KL
4075 *
4076 * Finally, these sequences break the assumptions of
4077 * standard ECMA-48 style parsers as pointed out at
4078 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4079 * Therefore in order to keep a clean display, we cannot
4080 * parse anything else in this sequence.
4081 */
051e2913
KL
4082 sgrColorMode = 38;
4083 continue;
14c78e1b
KL
4084 } else {
4085 // Underscore on, default foreground color
4086 currentState.attr.setUnderline(true);
4087 currentState.attr.setForeColor(Color.WHITE);
4088 }
34a42e78
KL
4089 break;
4090 case 39:
4091 // Underscore off, default foreground color
4092 currentState.attr.setUnderline(false);
4093 currentState.attr.setForeColor(Color.WHITE);
051e2913 4094 currentState.attr.setForeColorRGB(-1);
34a42e78
KL
4095 break;
4096 case 40:
4097 // Set black background
4098 currentState.attr.setBackColor(Color.BLACK);
051e2913 4099 currentState.attr.setBackColorRGB(-1);
34a42e78
KL
4100 break;
4101 case 41:
4102 // Set red background
4103 currentState.attr.setBackColor(Color.RED);
051e2913 4104 currentState.attr.setBackColorRGB(-1);
34a42e78
KL
4105 break;
4106 case 42:
4107 // Set green background
4108 currentState.attr.setBackColor(Color.GREEN);
051e2913 4109 currentState.attr.setBackColorRGB(-1);
34a42e78
KL
4110 break;
4111 case 43:
4112 // Set yellow background
4113 currentState.attr.setBackColor(Color.YELLOW);
051e2913 4114 currentState.attr.setBackColorRGB(-1);
34a42e78
KL
4115 break;
4116 case 44:
4117 // Set blue background
4118 currentState.attr.setBackColor(Color.BLUE);
051e2913 4119 currentState.attr.setBackColorRGB(-1);
34a42e78
KL
4120 break;
4121 case 45:
4122 // Set magenta background
4123 currentState.attr.setBackColor(Color.MAGENTA);
051e2913 4124 currentState.attr.setBackColorRGB(-1);
34a42e78
KL
4125 break;
4126 case 46:
4127 // Set cyan background
4128 currentState.attr.setBackColor(Color.CYAN);
051e2913 4129 currentState.attr.setBackColorRGB(-1);
34a42e78
KL
4130 break;
4131 case 47:
4132 // Set white background
4133 currentState.attr.setBackColor(Color.WHITE);
051e2913 4134 currentState.attr.setBackColorRGB(-1);
34a42e78 4135 break;
14c78e1b
KL
4136 case 48:
4137 if (type == DeviceType.XTERM) {
4138 /*
4139 * Xterm supports T.416 / ISO-8613-3 codes to select
4140 * either an indexed color or an RGB value. (It also
4141 * permits these ISO-8613-3 SGR sequences to be separated
4142 * by colons rather than semicolons.)
4143 *
051e2913
KL
4144 * We will support only the following:
4145 *
4146 * 1. Indexed color mode (88- or 256-color modes).
4147 *
4148 * 2. Direct RGB.
4149 *
4150 * These cover most of the use cases in the real world.
4151 *
4152 * HOWEVER, note that this is an awful broken "standard",
4153 * with no way to do it "right". See
4154 * http://invisible-island.net/ncurses/ncurses.faq.html#xterm_16MegaColors
4155 * for a detailed discussion of the current state of RGB
4156 * in various terminals, the point of which is that none
4157 * of them really do the same thing despite all appearing
4158 * to be "xterm".
4159 *
4160 * Also see
4161 * https://bugs.kde.org/show_bug.cgi?id=107487#c3 .
4162 * where it is assumed that supporting just the "indexed
4163 * mode" of these sequences (which could align easily
4164 * with existing SGR colors) is assumed to mean full
4165 * support of 24-bit RGB. So it is all or nothing.
4166 *
4167 * Finally, these sequences break the assumptions of
4168 * standard ECMA-48 style parsers as pointed out at
4169 * https://bugs.kde.org/show_bug.cgi?id=107487#c11 .
4170 * Therefore in order to keep a clean display, we cannot
4171 * parse anything else in this sequence.
14c78e1b 4172 */
051e2913
KL
4173 sgrColorMode = 48;
4174 continue;
14c78e1b
KL
4175 }
4176 break;
34a42e78
KL
4177 case 49:
4178 // Default background
4179 currentState.attr.setBackColor(Color.BLACK);
051e2913 4180 currentState.attr.setBackColorRGB(-1);
34a42e78
KL
4181 break;
4182
4183 default:
4184 break;
4185 }
4186 }
4187 }
4188
4189 /**
4190 * DA - Device attributes.
4191 */
4192 private void da() {
4193 int extendedFlag = 0;
4194 int i = 0;
bd8d51fa
KL
4195 if (collectBuffer.length() > 0) {
4196 String args = collectBuffer.substring(1);
4197 if (collectBuffer.charAt(0) == '>') {
34a42e78 4198 extendedFlag = 1;
bd8d51fa 4199 if (collectBuffer.length() >= 2) {
c88c4ced 4200 i = Integer.parseInt(args);
34a42e78 4201 }
bd8d51fa 4202 } else if (collectBuffer.charAt(0) == '=') {
34a42e78 4203 extendedFlag = 2;
bd8d51fa 4204 if (collectBuffer.length() >= 2) {
c88c4ced 4205 i = Integer.parseInt(args);
34a42e78
KL
4206 }
4207 } else {
4208 // Unknown code, bail out
4209 return;
4210 }
4211 }
4212
4213 if ((i != 0) && (i != 1)) {
4214 return;
4215 }
4216
4217 if ((extendedFlag == 0) && (i == 0)) {
4218 // Send string directly to remote side
4219 writeRemote(deviceTypeResponse());
4220 return;
4221 }
4222
4223 if ((type == DeviceType.VT220) || (type == DeviceType.XTERM)) {
4224
4225 if ((extendedFlag == 1) && (i == 0)) {
4226 /*
4227 * Request "What type of terminal are you, what is your
4228 * firmware version, and what hardware options do you have
4229 * installed?"
4230 *
4231 * Respond: "I am a VT220 (identification code of 1), my
4232 * firmware version is _____ (Pv), and I have _____ Po
4233 * options installed."
4234 *
4235 * (Same as xterm)
4236 *
4237 */
4238
4239 if (s8c1t == true) {
4240 writeRemote("\u009b>1;10;0c");
4241 } else {
4242 writeRemote("\033[>1;10;0c");
4243 }
4244 }
4245 }
4246
4247 // VT420 and up
4248 if ((extendedFlag == 2) && (i == 0)) {
4249
4250 /*
4251 * Request "What is your unit ID?"
4252 *
4253 * Respond: "I was manufactured at site 00 and have a unique ID
4254 * number of 123."
4255 *
4256 */
4257 writeRemote("\033P!|00010203\033\\");
4258 }
4259 }
4260
4261 /**
4262 * DECSTBM - Set top and bottom margins.
4263 */
4264 private void decstbm() {
36338168 4265 boolean decPrivateModeFlag = false;
34a42e78 4266
36338168
KL
4267 for (int i = 0; i < collectBuffer.length(); i++) {
4268 if (collectBuffer.charAt(i) == '?') {
4269 decPrivateModeFlag = true;
4270 break;
4271 }
34a42e78 4272 }
36338168
KL
4273 if (decPrivateModeFlag) {
4274 // This could be restore DEC private mode values.
4275 // Ignore it.
4276 } else {
4277 // DECSTBM
4278 int top = getCsiParam(0, 1, 1, height) - 1;
4279 int bottom = getCsiParam(1, height, 1, height) - 1;
34a42e78 4280
36338168
KL
4281 if (top > bottom) {
4282 top = bottom;
4283 }
4284 scrollRegionTop = top;
4285 scrollRegionBottom = bottom;
4286
4287 // Home cursor
4288 cursorPosition(0, 0);
4289 }
34a42e78
KL
4290 }
4291
4292 /**
4293 * DECREQTPARM - Request terminal parameters.
4294 */
4295 private void decreqtparm() {
4296 int i = getCsiParam(0, 0);
4297
4298 if ((i != 0) && (i != 1)) {
4299 return;
4300 }
4301
4302 String str = "";
4303
4304 /*
4305 * Request terminal parameters.
4306 *
4307 * Respond with:
4308 *
4309 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
4310 * (CLoCk MULtiplier = 1, STP option flags = 0)
4311 *
4312 * (Same as xterm)
4313 */
4314 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4315 && (s8c1t == true)
4316 ) {
4317 str = String.format("\u009b%d;1;1;128;128;1;0x", i + 2);
4318 } else {
4319 str = String.format("\033[%d;1;1;128;128;1;0x", i + 2);
4320 }
4321 writeRemote(str);
4322 }
4323
4324 /**
4325 * DECSCA - Select Character Attributes.
4326 */
4327 private void decsca() {
4328 int i = getCsiParam(0, 0);
4329
4330 if ((i == 0) || (i == 2)) {
4331 // Protect mode OFF
4332 currentState.attr.setProtect(false);
4333 }
4334 if (i == 1) {
4335 // Protect mode ON
4336 currentState.attr.setProtect(true);
4337 }
4338 }
4339
4340 /**
4341 * DECSTR - Soft Terminal Reset.
4342 */
4343 private void decstr() {
4344 // Do exactly like RIS - Reset to initial state
4345 reset();
4346 // Do I clear screen too? I think so...
4347 eraseScreen(0, 0, height - 1, width - 1, false);
4348 cursorPosition(0, 0);
4349 }
4350
4351 /**
4352 * DSR - Device status report.
4353 */
4354 private void dsr() {
4355 boolean decPrivateModeFlag = false;
c8165631 4356 int row = currentState.cursorY;
34a42e78 4357
bd8d51fa
KL
4358 for (int i = 0; i < collectBuffer.length(); i++) {
4359 if (collectBuffer.charAt(i) == '?') {
34a42e78 4360 decPrivateModeFlag = true;
bd8d51fa 4361 break;
34a42e78
KL
4362 }
4363 }
4364
4365 int i = getCsiParam(0, 0);
4366
4367 switch (i) {
4368
4369 case 5:
4370 // Request status report. Respond with "OK, no malfunction."
4371
4372 // Send string directly to remote side
4373 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4374 && (s8c1t == true)
4375 ) {
4376 writeRemote("\u009b0n");
4377 } else {
4378 writeRemote("\033[0n");
4379 }
4380 break;
4381
4382 case 6:
4383 // Request cursor position. Respond with current position.
c8165631
KL
4384 if (currentState.originMode == true) {
4385 row -= scrollRegionTop;
4386 }
34a42e78
KL
4387 String str = "";
4388 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4389 && (s8c1t == true)
4390 ) {
c8165631
KL
4391 str = String.format("\u009b%d;%dR", row + 1,
4392 currentState.cursorX + 1);
34a42e78 4393 } else {
c8165631
KL
4394 str = String.format("\033[%d;%dR", row + 1,
4395 currentState.cursorX + 1);
34a42e78
KL
4396 }
4397
4398 // Send string directly to remote side
4399 writeRemote(str);
4400 break;
4401
4402 case 15:
4403 if (decPrivateModeFlag == true) {
4404
4405 // Request printer status report. Respond with "Printer not
4406 // connected."
4407
4408 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4409 && (s8c1t == true)) {
4410 writeRemote("\u009b?13n");
4411 } else {
4412 writeRemote("\033[?13n");
4413 }
4414 }
4415 break;
4416
4417 case 25:
4418 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4419 && (decPrivateModeFlag == true)
4420 ) {
4421
4422 // Request user-defined keys are locked or unlocked. Respond
4423 // with "User-defined keys are locked."
4424
4425 if (s8c1t == true) {
4426 writeRemote("\u009b?21n");
4427 } else {
4428 writeRemote("\033[?21n");
4429 }
4430 }
4431 break;
4432
4433 case 26:
4434 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
4435 && (decPrivateModeFlag == true)
4436 ) {
4437
4438 // Request keyboard language. Respond with "Keyboard
4439 // language is North American."
4440
4441 if (s8c1t == true) {
4442 writeRemote("\u009b?27;1n");
4443 } else {
4444 writeRemote("\033[?27;1n");
4445 }
4446
4447 }
4448 break;
4449
4450 default:
4451 // Some other option, ignore
4452 break;
4453 }
4454 }
4455
4456 /**
4457 * TBC - Tabulation clear.
4458 */
4459 private void tbc() {
4460 int i = getCsiParam(0, 0);
4461 if (i == 0) {
4462 List<Integer> newStops = new ArrayList<Integer>();
4463 for (Integer stop: tabStops) {
4464 if (stop == currentState.cursorX) {
4465 continue;
4466 }
4467 newStops.add(stop);
4468 }
4469 tabStops = newStops;
4470 }
4471 if (i == 3) {
4472 tabStops.clear();
4473 }
4474 }
4475
4476 /**
4477 * Erase the characters in the current line from the start column to the
4478 * end column, inclusive.
4479 *
4480 * @param start starting column to erase (between 0 and width - 1)
4481 * @param end ending column to erase (between 0 and width - 1)
4482 * @param honorProtected if true, do not erase characters with the
4483 * protected attribute set
4484 */
4485 private void eraseLine(int start, int end, final boolean honorProtected) {
4486
4487 if (start > end) {
4488 return;
4489 }
4490 if (end > width - 1) {
4491 end = width - 1;
4492 }
4493 if (start < 0) {
4494 start = 0;
4495 }
4496
4497 for (int i = start; i <= end; i++) {
4498 DisplayLine line = display.get(currentState.cursorY);
4499 if ((!honorProtected)
7c870d89 4500 || ((honorProtected) && (!line.charAt(i).isProtect()))) {
34a42e78
KL
4501
4502 switch (type) {
4503 case VT100:
4504 case VT102:
4505 case VT220:
4506 /*
4507 * From the VT102 manual:
4508 *
4509 * Erasing a character also erases any character
4510 * attribute of the character.
4511 */
4512 line.setBlank(i);
4513 break;
4514 case XTERM:
4515 /*
4516 * Erase with the current color a.k.a. back-color erase
4517 * (bce).
4518 */
4519 line.setChar(i, ' ');
4520 line.setAttr(i, currentState.attr);
4521 break;
4522 }
4523 }
4524 }
4525 }
4526
4527 /**
4528 * Erase a rectangular section of the screen, inclusive. end column,
4529 * inclusive.
4530 *
4531 * @param startRow starting row to erase (between 0 and height - 1)
4532 * @param startCol starting column to erase (between 0 and width - 1)
4533 * @param endRow ending row to erase (between 0 and height - 1)
4534 * @param endCol ending column to erase (between 0 and width - 1)
4535 * @param honorProtected if true, do not erase characters with the
4536 * protected attribute set
4537 */
4538 private void eraseScreen(final int startRow, final int startCol,
4539 final int endRow, final int endCol, final boolean honorProtected) {
4540
4541 int oldCursorY;
4542
4543 if ((startRow < 0)
4544 || (startCol < 0)
4545 || (endRow < 0)
4546 || (endCol < 0)
4547 || (endRow < startRow)
4548 || (endCol < startCol)
4549 ) {
4550 return;
4551 }
4552
4553 oldCursorY = currentState.cursorY;
4554 for (int i = startRow; i <= endRow; i++) {
4555 currentState.cursorY = i;
4556 eraseLine(startCol, endCol, honorProtected);
4557
4558 // Erase display clears the double attributes
4559 display.get(i).setDoubleWidth(false);
4560 display.get(i).setDoubleHeight(0);
4561 }
4562 currentState.cursorY = oldCursorY;
4563 }
4564
4565 /**
4566 * VT220 printer functions. All of these are parsed, but won't do
4567 * anything.
4568 */
4569 private void printerFunctions() {
4570 boolean decPrivateModeFlag = false;
bd8d51fa
KL
4571 for (int i = 0; i < collectBuffer.length(); i++) {
4572 if (collectBuffer.charAt(i) == '?') {
34a42e78 4573 decPrivateModeFlag = true;
bd8d51fa 4574 break;
34a42e78
KL
4575 }
4576 }
4577
4578 int i = getCsiParam(0, 0);
4579
4580 switch (i) {
4581
4582 case 0:
4583 if (decPrivateModeFlag == false) {
4584 // Print screen
4585 }
4586 break;
4587
4588 case 1:
4589 if (decPrivateModeFlag == true) {
4590 // Print cursor line
4591 }
4592 break;
4593
4594 case 4:
4595 if (decPrivateModeFlag == true) {
4596 // Auto print mode OFF
4597 } else {
4598 // Printer controller OFF
4599
4600 // Characters re-appear on the screen
4601 printerControllerMode = false;
4602 }
4603 break;
4604
4605 case 5:
4606 if (decPrivateModeFlag == true) {
4607 // Auto print mode
4608
4609 } else {
4610 // Printer controller
4611
4612 // Characters get sucked into oblivion
4613 printerControllerMode = true;
4614 }
4615 break;
4616
4617 default:
4618 break;
4619
4620 }
4621 }
4622
4623 /**
4624 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
4625 * of remote systems will send an XTerm title sequence even if TERM isn't
4626 * xterm.
4627 *
4628 * @param xtermChar the character received from the remote side
4629 */
4630 private void oscPut(final char xtermChar) {
051e2913
KL
4631 // System.err.println("oscPut: " + xtermChar);
4632
34a42e78 4633 // Collect first
bd8d51fa 4634 collectBuffer.append(xtermChar);
34a42e78
KL
4635
4636 // Xterm cases...
051e2913
KL
4637 if ((xtermChar == 0x07)
4638 || (collectBuffer.toString().endsWith("\033\\"))
4639 ) {
4640 String args = null;
4641 if (xtermChar == 0x07) {
4642 args = collectBuffer.substring(0, collectBuffer.length() - 1);
4643 } else {
4644 args = collectBuffer.substring(0, collectBuffer.length() - 2);
4645 }
4646
c88c4ced 4647 String [] p = args.split(";");
34a42e78
KL
4648 if (p.length > 0) {
4649 if ((p[0].equals("0")) || (p[0].equals("2"))) {
4650 if (p.length > 1) {
4651 // Screen title
4652 screenTitle = p[1];
4653 }
4654 }
051e2913
KL
4655
4656 if (p[0].equals("4")) {
4657 for (int i = 1; i + 1 < p.length; i += 2) {
4658 // Set a color index value
4659 try {
4660 set88Color(Integer.parseInt(p[i]), p[i + 1]);
4661 } catch (NumberFormatException e) {
4662 // SQUASH
4663 }
4664 }
4665 }
34a42e78
KL
4666 }
4667
4668 // Go to SCAN_GROUND state
4669 toGround();
4670 return;
4671 }
4672 }
4673
978a5d8f
KL
4674 /**
4675 * Handle the SCAN_SOSPMAPC_STRING state. This is currently only used by
4676 * Jexer ECMA48Terminal to talk to ECMA48.
4677 *
4678 * @param pmChar the character received from the remote side
4679 */
4680 private void pmPut(final char pmChar) {
4681 // System.err.println("pmPut: " + pmChar);
4682
4683 // Collect first
4684 collectBuffer.append(pmChar);
4685
4686 // Xterm cases...
4687 if (collectBuffer.toString().endsWith("\033\\")) {
4688 String arg = null;
4689 arg = collectBuffer.substring(0, collectBuffer.length() - 2);
4690
4691 // System.err.println("arg: '" + arg + "'");
4692
4693 if (arg.equals("hideMousePointer")) {
4694 hideMousePointer = true;
4695 }
4696 if (arg.equals("showMousePointer")) {
4697 hideMousePointer = false;
4698 }
4699
4700 // Go to SCAN_GROUND state
4701 toGround();
4702 return;
4703 }
4704 }
4705
69a8c368
KL
4706 /**
4707 * Perform xterm window operations.
4708 */
4709 private void xtermWindowOps() {
4710 boolean xtermPrivateModeFlag = false;
4711
4712 for (int i = 0; i < collectBuffer.length(); i++) {
4713 if (collectBuffer.charAt(i) == '?') {
4714 xtermPrivateModeFlag = true;
4715 break;
4716 }
4717 }
4718
4719 int i = getCsiParam(0, 0);
4720
4721 if (!xtermPrivateModeFlag) {
4722 if (i == 14) {
4723 // Report xterm window in pixels as CSI 4 ; height ; width t
4724 writeRemote(String.format("\033[4;%d;%dt", textHeight * height,
4725 textWidth * width));
4726 }
4727 }
4728 }
4729
34a42e78
KL
4730 /**
4731 * Run this input character through the ECMA48 state machine.
4732 *
4733 * @param ch character from the remote side
4734 */
cf9af8df 4735 private void consume(char ch) {
34a42e78
KL
4736
4737 // DEBUG
5fc7bf09 4738 // System.err.printf("%c STATE = %s\n", ch, scanState);
34a42e78
KL
4739
4740 // Special case for VT10x: 7-bit characters only
4741 if ((type == DeviceType.VT100) || (type == DeviceType.VT102)) {
4742 ch = (char)(ch & 0x7F);
4743 }
4744
4745 // Special "anywhere" states
4746
4747 // 18, 1A --> execute, then switch to SCAN_GROUND
4748 if ((ch == 0x18) || (ch == 0x1A)) {
4749 // CAN and SUB abort escape sequences
4750 toGround();
4751 return;
4752 }
4753
4754 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
4755
4756 // 0x1B == ESCAPE
051e2913
KL
4757 if (ch == 0x1B) {
4758 if ((type == DeviceType.XTERM)
978a5d8f 4759 && ((scanState == ScanState.OSC_STRING)
5fc7bf09 4760 || (scanState == ScanState.DCS_SIXEL)
978a5d8f 4761 || (scanState == ScanState.SOSPMAPC_STRING))
051e2913
KL
4762 ) {
4763 // Xterm can pass ESCAPE to its OSC sequence.
5fc7bf09 4764 // Xterm can pass ESCAPE to its DCS sequence.
978a5d8f 4765 // Jexer can pass ESCAPE to its PM sequence.
051e2913
KL
4766 } else if ((scanState != ScanState.DCS_ENTRY)
4767 && (scanState != ScanState.DCS_INTERMEDIATE)
4768 && (scanState != ScanState.DCS_IGNORE)
4769 && (scanState != ScanState.DCS_PARAM)
4770 && (scanState != ScanState.DCS_PASSTHROUGH)
4771 ) {
051e2913
KL
4772 scanState = ScanState.ESCAPE;
4773 return;
4774 }
34a42e78
KL
4775 }
4776
4777 // 0x9B == CSI 8-bit sequence
4778 if (ch == 0x9B) {
4779 scanState = ScanState.CSI_ENTRY;
4780 return;
4781 }
4782
4783 // 0x9D goes to ScanState.OSC_STRING
4784 if (ch == 0x9D) {
4785 scanState = ScanState.OSC_STRING;
4786 return;
4787 }
4788
4789 // 0x90 goes to DCS_ENTRY
4790 if (ch == 0x90) {
4791 scanState = ScanState.DCS_ENTRY;
4792 return;
4793 }
4794
4795 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
4796 if ((ch == 0x98) || (ch == 0x9E) || (ch == 0x9F)) {
4797 scanState = ScanState.SOSPMAPC_STRING;
4798 return;
4799 }
4800
4801 // 0x7F (DEL) is always discarded
4802 if (ch == 0x7F) {
4803 return;
4804 }
4805
4806 switch (scanState) {
4807
4808 case GROUND:
4809 // 00-17, 19, 1C-1F --> execute
4810 // 80-8F, 91-9A, 9C --> execute
4811 if ((ch <= 0x1F) || ((ch >= 0x80) && (ch <= 0x9F))) {
4812 handleControlChar(ch);
4813 }
4814
4815 // 20-7F --> print
4816 if (((ch >= 0x20) && (ch <= 0x7F))
4817 || (ch >= 0xA0)
4818 ) {
4819
4820 // VT220 printer --> trash bin
4821 if (((type == DeviceType.VT220)
4822 || (type == DeviceType.XTERM))
4823 && (printerControllerMode == true)
4824 ) {
4825 return;
4826 }
4827
4828 // Hang onto this character
4829 repCh = mapCharacter(ch);
4830
4831 // Print this character
4832 printCharacter(repCh);
4833 }
4834 return;
4835
4836 case ESCAPE:
4837 // 00-17, 19, 1C-1F --> execute
4838 if (ch <= 0x1F) {
4839 handleControlChar(ch);
4840 return;
4841 }
4842
4843 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
4844 if ((ch >= 0x20) && (ch <= 0x2F)) {
4845 collect(ch);
4846 scanState = ScanState.ESCAPE_INTERMEDIATE;
4847 return;
4848 }
4849
4850 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
4851 if ((ch >= 0x30) && (ch <= 0x4F)) {
4852 switch (ch) {
4853 case '0':
4854 case '1':
4855 case '2':
4856 case '3':
4857 case '4':
4858 case '5':
4859 case '6':
4860 break;
4861 case '7':
4862 // DECSC - Save cursor
4863 // Note this code overlaps both ANSI and VT52 mode
4864 decsc();
4865 break;
4866
4867 case '8':
4868 // DECRC - Restore cursor
4869 // Note this code overlaps both ANSI and VT52 mode
4870 decrc();
4871 break;
4872
4873 case '9':
4874 case ':':
4875 case ';':
4876 break;
4877 case '<':
4878 if (vt52Mode == true) {
4879 // DECANM - Enter ANSI mode
4880 vt52Mode = false;
4881 arrowKeyMode = ArrowKeyMode.VT100;
4882
4883 /*
4884 * From the VT102 docs: "You use ANSI mode to select
4885 * most terminal features; the terminal uses the same
4886 * features when it switches to VT52 mode. You
4887 * cannot, however, change most of these features in
4888 * VT52 mode."
4889 *
4890 * In other words, do not reset any other attributes
4891 * when switching between VT52 submode and ANSI.
4892 */
4893
4894 // Reset fonts
4895 currentState.g0Charset = CharacterSet.US;
4896 currentState.g1Charset = CharacterSet.DRAWING;
4897 s8c1t = false;
4898 singleshift = Singleshift.NONE;
4899 currentState.glLockshift = LockshiftMode.NONE;
4900 currentState.grLockshift = LockshiftMode.NONE;
4901 }
4902 break;
4903 case '=':
4904 // DECKPAM - Keypad application mode
4905 // Note this code overlaps both ANSI and VT52 mode
4906 deckpam();
4907 break;
4908 case '>':
4909 // DECKPNM - Keypad numeric mode
4910 // Note this code overlaps both ANSI and VT52 mode
4911 deckpnm();
4912 break;
4913 case '?':
4914 case '@':
4915 break;
4916 case 'A':
4917 if (vt52Mode == true) {
4918 // Cursor up, and stop at the top without scrolling
4919 cursorUp(1, false);
4920 }
4921 break;
4922 case 'B':
4923 if (vt52Mode == true) {
4924 // Cursor down, and stop at the bottom without scrolling
4925 cursorDown(1, false);
4926 }
4927 break;
4928 case 'C':
4929 if (vt52Mode == true) {
4930 // Cursor right, and stop at the right without scrolling
4931 cursorRight(1, false);
4932 }
4933 break;
4934 case 'D':
4935 if (vt52Mode == true) {
4936 // Cursor left, and stop at the left without scrolling
4937 cursorLeft(1, false);
4938 } else {
4939 // IND - Index
4940 ind();
4941 }
4942 break;
4943 case 'E':
4944 if (vt52Mode == true) {
4945 // Nothing
4946 } else {
4947 // NEL - Next line
4948 nel();
4949 }
4950 break;
4951 case 'F':
4952 if (vt52Mode == true) {
4953 // G0 --> Special graphics
4954 currentState.g0Charset = CharacterSet.VT52_GRAPHICS;
4955 }
4956 break;
4957 case 'G':
4958 if (vt52Mode == true) {
4959 // G0 --> ASCII set
4960 currentState.g0Charset = CharacterSet.US;
4961 }
4962 break;
4963 case 'H':
4964 if (vt52Mode == true) {
4965 // Cursor to home
4966 cursorPosition(0, 0);
4967 } else {
4968 // HTS - Horizontal tabulation set
4969 hts();
4970 }
4971 break;
4972 case 'I':
4973 if (vt52Mode == true) {
4974 // Reverse line feed. Same as RI.
4975 ri();
4976 }
4977 break;
4978 case 'J':
4979 if (vt52Mode == true) {
4980 // Erase to end of screen
4981 eraseLine(currentState.cursorX, width - 1, false);
4982 eraseScreen(currentState.cursorY + 1, 0, height - 1,
4983 width - 1, false);
4984 }
4985 break;
4986 case 'K':
4987 if (vt52Mode == true) {
4988 // Erase to end of line
4989 eraseLine(currentState.cursorX, width - 1, false);
4990 }
4991 break;
4992 case 'L':
4993 break;
4994 case 'M':
4995 if (vt52Mode == true) {
4996 // Nothing
4997 } else {
4998 // RI - Reverse index
4999 ri();
5000 }
5001 break;
5002 case 'N':
5003 if (vt52Mode == false) {
5004 // SS2
5005 singleshift = Singleshift.SS2;
5006 }
5007 break;
5008 case 'O':
5009 if (vt52Mode == false) {
5010 // SS3
5011 singleshift = Singleshift.SS3;
5012 }
5013 break;
5014 }
5015 toGround();
5016 return;
5017 }
5018 if ((ch >= 0x51) && (ch <= 0x57)) {
5019 switch (ch) {
5020 case 'Q':
5021 case 'R':
5022 case 'S':
5023 case 'T':
5024 case 'U':
5025 case 'V':
5026 case 'W':
5027 break;
5028 }
5029 toGround();
5030 return;
5031 }
5032 if (ch == 0x59) {
5033 // 'Y'
5034 if (vt52Mode == true) {
5035 scanState = ScanState.VT52_DIRECT_CURSOR_ADDRESS;
5036 } else {
5037 toGround();
5038 }
5039 return;
5040 }
5041 if (ch == 0x5A) {
5042 // 'Z'
5043 if (vt52Mode == true) {
5044 // Identify
5045 // Send string directly to remote side
5046 writeRemote("\033/Z");
5047 } else {
5048 // DECID
5049 // Send string directly to remote side
5050 writeRemote(deviceTypeResponse());
5051 }
5052 toGround();
5053 return;
5054 }
5055 if (ch == 0x5C) {
5056 // '\'
5057 toGround();
5058 return;
5059 }
5060
5061 // VT52 cannot get to any of these other states
5062 if (vt52Mode == true) {
5063 toGround();
5064 return;
5065 }
5066
5067 if ((ch >= 0x60) && (ch <= 0x7E)) {
5068 switch (ch) {
5069 case '`':
5070 case 'a':
5071 case 'b':
5072 break;
5073 case 'c':
5074 // RIS - Reset to initial state
5075 reset();
5076 // Do I clear screen too? I think so...
5077 eraseScreen(0, 0, height - 1, width - 1, false);
5078 cursorPosition(0, 0);
5079 break;
5080 case 'd':
5081 case 'e':
5082 case 'f':
5083 case 'g':
5084 case 'h':
5085 case 'i':
5086 case 'j':
5087 case 'k':
5088 case 'l':
5089 case 'm':
5090 break;
5091 case 'n':
5092 if ((type == DeviceType.VT220)
5093 || (type == DeviceType.XTERM)) {
5094
5095 // VT220 lockshift G2 into GL
5096 currentState.glLockshift = LockshiftMode.G2_GL;
5097 shiftOut = false;
5098 }
5099 break;
5100 case 'o':
5101 if ((type == DeviceType.VT220)
5102 || (type == DeviceType.XTERM)) {
5103
5104 // VT220 lockshift G3 into GL
5105 currentState.glLockshift = LockshiftMode.G3_GL;
5106 shiftOut = false;
5107 }
5108 break;
5109 case 'p':
5110 case 'q':
5111 case 'r':
5112 case 's':
5113 case 't':
5114 case 'u':
5115 case 'v':
5116 case 'w':
5117 case 'x':
5118 case 'y':
5119 case 'z':
5120 case '{':
5121 break;
5122 case '|':
5123 if ((type == DeviceType.VT220)
5124 || (type == DeviceType.XTERM)) {
5125
5126 // VT220 lockshift G3 into GR
5127 currentState.grLockshift = LockshiftMode.G3_GR;
5128 shiftOut = false;
5129 }
5130 break;
5131 case '}':
5132 if ((type == DeviceType.VT220)
5133 || (type == DeviceType.XTERM)) {
5134
5135 // VT220 lockshift G2 into GR
5136 currentState.grLockshift = LockshiftMode.G2_GR;
5137 shiftOut = false;
5138 }
5139 break;
5140
5141 case '~':
5142 if ((type == DeviceType.VT220)
5143 || (type == DeviceType.XTERM)) {
5144
5145 // VT220 lockshift G1 into GR
5146 currentState.grLockshift = LockshiftMode.G1_GR;
5147 shiftOut = false;
5148 }
5149 break;
5150 }
5151 toGround();
5152 }
5153
5154 // 7F --> ignore
5155
5156 // 0x5B goes to CSI_ENTRY
5157 if (ch == 0x5B) {
5158 scanState = ScanState.CSI_ENTRY;
5159 }
5160
5161 // 0x5D goes to OSC_STRING
5162 if (ch == 0x5D) {
5163 scanState = ScanState.OSC_STRING;
5164 }
5165
5166 // 0x50 goes to DCS_ENTRY
5167 if (ch == 0x50) {
5168 scanState = ScanState.DCS_ENTRY;
5169 }
5170
5171 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
5172 if ((ch == 0x58) || (ch == 0x5E) || (ch == 0x5F)) {
5173 scanState = ScanState.SOSPMAPC_STRING;
5174 }
5175
5176 return;
5177
5178 case ESCAPE_INTERMEDIATE:
5179 // 00-17, 19, 1C-1F --> execute
5180 if (ch <= 0x1F) {
5181 handleControlChar(ch);
5182 }
5183
5184 // 20-2F --> collect
5185 if ((ch >= 0x20) && (ch <= 0x2F)) {
5186 collect(ch);
5187 }
5188
5189 // 30-7E --> dispatch, then switch to GROUND
5190 if ((ch >= 0x30) && (ch <= 0x7E)) {
5191 switch (ch) {
5192 case '0':
bd8d51fa
KL
5193 if ((collectBuffer.length() == 1)
5194 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5195 // G0 --> Special graphics
5196 currentState.g0Charset = CharacterSet.DRAWING;
5197 }
bd8d51fa
KL
5198 if ((collectBuffer.length() == 1)
5199 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5200 // G1 --> Special graphics
5201 currentState.g1Charset = CharacterSet.DRAWING;
5202 }
5203 if ((type == DeviceType.VT220)
5204 || (type == DeviceType.XTERM)) {
5205
bd8d51fa
KL
5206 if ((collectBuffer.length() == 1)
5207 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5208 // G2 --> Special graphics
5209 currentState.g2Charset = CharacterSet.DRAWING;
5210 }
bd8d51fa
KL
5211 if ((collectBuffer.length() == 1)
5212 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5213 // G3 --> Special graphics
5214 currentState.g3Charset = CharacterSet.DRAWING;
5215 }
5216 }
5217 break;
5218 case '1':
bd8d51fa
KL
5219 if ((collectBuffer.length() == 1)
5220 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5221 // G0 --> Alternate character ROM standard character set
5222 currentState.g0Charset = CharacterSet.ROM;
5223 }
bd8d51fa
KL
5224 if ((collectBuffer.length() == 1)
5225 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5226 // G1 --> Alternate character ROM standard character set
5227 currentState.g1Charset = CharacterSet.ROM;
5228 }
5229 break;
5230 case '2':
bd8d51fa
KL
5231 if ((collectBuffer.length() == 1)
5232 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5233 // G0 --> Alternate character ROM special graphics
5234 currentState.g0Charset = CharacterSet.ROM_SPECIAL;
5235 }
bd8d51fa
KL
5236 if ((collectBuffer.length() == 1)
5237 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5238 // G1 --> Alternate character ROM special graphics
5239 currentState.g1Charset = CharacterSet.ROM_SPECIAL;
5240 }
5241 break;
5242 case '3':
bd8d51fa
KL
5243 if ((collectBuffer.length() == 1)
5244 && (collectBuffer.charAt(0) == '#')) {
34a42e78
KL
5245 // DECDHL - Double-height line (top half)
5246 dechdl(true);
5247 }
5248 break;
5249 case '4':
bd8d51fa
KL
5250 if ((collectBuffer.length() == 1)
5251 && (collectBuffer.charAt(0) == '#')) {
34a42e78
KL
5252 // DECDHL - Double-height line (bottom half)
5253 dechdl(false);
5254 }
5255 if ((type == DeviceType.VT220)
5256 || (type == DeviceType.XTERM)) {
5257
bd8d51fa
KL
5258 if ((collectBuffer.length() == 1)
5259 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5260 // G0 --> DUTCH
5261 currentState.g0Charset = CharacterSet.NRC_DUTCH;
5262 }
bd8d51fa
KL
5263 if ((collectBuffer.length() == 1)
5264 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5265 // G1 --> DUTCH
5266 currentState.g1Charset = CharacterSet.NRC_DUTCH;
5267 }
bd8d51fa
KL
5268 if ((collectBuffer.length() == 1)
5269 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5270 // G2 --> DUTCH
5271 currentState.g2Charset = CharacterSet.NRC_DUTCH;
5272 }
bd8d51fa
KL
5273 if ((collectBuffer.length() == 1)
5274 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5275 // G3 --> DUTCH
5276 currentState.g3Charset = CharacterSet.NRC_DUTCH;
5277 }
5278 }
5279 break;
5280 case '5':
bd8d51fa
KL
5281 if ((collectBuffer.length() == 1)
5282 && (collectBuffer.charAt(0) == '#')) {
34a42e78
KL
5283 // DECSWL - Single-width line
5284 decswl();
5285 }
5286 if ((type == DeviceType.VT220)
5287 || (type == DeviceType.XTERM)) {
5288
bd8d51fa
KL
5289 if ((collectBuffer.length() == 1)
5290 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5291 // G0 --> FINNISH
5292 currentState.g0Charset = CharacterSet.NRC_FINNISH;
5293 }
bd8d51fa
KL
5294 if ((collectBuffer.length() == 1)
5295 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5296 // G1 --> FINNISH
5297 currentState.g1Charset = CharacterSet.NRC_FINNISH;
5298 }
bd8d51fa
KL
5299 if ((collectBuffer.length() == 1)
5300 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5301 // G2 --> FINNISH
5302 currentState.g2Charset = CharacterSet.NRC_FINNISH;
5303 }
bd8d51fa
KL
5304 if ((collectBuffer.length() == 1)
5305 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5306 // G3 --> FINNISH
5307 currentState.g3Charset = CharacterSet.NRC_FINNISH;
5308 }
5309 }
5310 break;
5311 case '6':
bd8d51fa
KL
5312 if ((collectBuffer.length() == 1)
5313 && (collectBuffer.charAt(0) == '#')) {
34a42e78
KL
5314 // DECDWL - Double-width line
5315 decdwl();
5316 }
5317 if ((type == DeviceType.VT220)
5318 || (type == DeviceType.XTERM)) {
5319
bd8d51fa
KL
5320 if ((collectBuffer.length() == 1)
5321 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5322 // G0 --> NORWEGIAN
5323 currentState.g0Charset = CharacterSet.NRC_NORWEGIAN;
5324 }
bd8d51fa
KL
5325 if ((collectBuffer.length() == 1)
5326 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5327 // G1 --> NORWEGIAN
5328 currentState.g1Charset = CharacterSet.NRC_NORWEGIAN;
5329 }
bd8d51fa
KL
5330 if ((collectBuffer.length() == 1)
5331 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5332 // G2 --> NORWEGIAN
5333 currentState.g2Charset = CharacterSet.NRC_NORWEGIAN;
5334 }
bd8d51fa
KL
5335 if ((collectBuffer.length() == 1)
5336 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5337 // G3 --> NORWEGIAN
5338 currentState.g3Charset = CharacterSet.NRC_NORWEGIAN;
5339 }
5340 }
5341 break;
5342 case '7':
5343 if ((type == DeviceType.VT220)
5344 || (type == DeviceType.XTERM)) {
5345
bd8d51fa
KL
5346 if ((collectBuffer.length() == 1)
5347 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5348 // G0 --> SWEDISH
5349 currentState.g0Charset = CharacterSet.NRC_SWEDISH;
5350 }
bd8d51fa
KL
5351 if ((collectBuffer.length() == 1)
5352 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5353 // G1 --> SWEDISH
5354 currentState.g1Charset = CharacterSet.NRC_SWEDISH;
5355 }
bd8d51fa
KL
5356 if ((collectBuffer.length() == 1)
5357 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5358 // G2 --> SWEDISH
5359 currentState.g2Charset = CharacterSet.NRC_SWEDISH;
5360 }
bd8d51fa
KL
5361 if ((collectBuffer.length() == 1)
5362 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5363 // G3 --> SWEDISH
5364 currentState.g3Charset = CharacterSet.NRC_SWEDISH;
5365 }
5366 }
5367 break;
5368 case '8':
bd8d51fa
KL
5369 if ((collectBuffer.length() == 1)
5370 && (collectBuffer.charAt(0) == '#')) {
34a42e78
KL
5371 // DECALN - Screen alignment display
5372 decaln();
5373 }
5374 break;
5375 case '9':
5376 case ':':
5377 case ';':
5378 break;
5379 case '<':
5380 if ((type == DeviceType.VT220)
5381 || (type == DeviceType.XTERM)) {
5382
bd8d51fa
KL
5383 if ((collectBuffer.length() == 1)
5384 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5385 // G0 --> DEC_SUPPLEMENTAL
5386 currentState.g0Charset = CharacterSet.DEC_SUPPLEMENTAL;
5387 }
bd8d51fa
KL
5388 if ((collectBuffer.length() == 1)
5389 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5390 // G1 --> DEC_SUPPLEMENTAL
5391 currentState.g1Charset = CharacterSet.DEC_SUPPLEMENTAL;
5392 }
bd8d51fa
KL
5393 if ((collectBuffer.length() == 1)
5394 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5395 // G2 --> DEC_SUPPLEMENTAL
5396 currentState.g2Charset = CharacterSet.DEC_SUPPLEMENTAL;
5397 }
bd8d51fa
KL
5398 if ((collectBuffer.length() == 1)
5399 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5400 // G3 --> DEC_SUPPLEMENTAL
5401 currentState.g3Charset = CharacterSet.DEC_SUPPLEMENTAL;
5402 }
5403 }
5404 break;
5405 case '=':
5406 if ((type == DeviceType.VT220)
5407 || (type == DeviceType.XTERM)) {
5408
bd8d51fa
KL
5409 if ((collectBuffer.length() == 1)
5410 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5411 // G0 --> SWISS
5412 currentState.g0Charset = CharacterSet.NRC_SWISS;
5413 }
bd8d51fa
KL
5414 if ((collectBuffer.length() == 1)
5415 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5416 // G1 --> SWISS
5417 currentState.g1Charset = CharacterSet.NRC_SWISS;
5418 }
bd8d51fa
KL
5419 if ((collectBuffer.length() == 1)
5420 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5421 // G2 --> SWISS
5422 currentState.g2Charset = CharacterSet.NRC_SWISS;
5423 }
bd8d51fa
KL
5424 if ((collectBuffer.length() == 1)
5425 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5426 // G3 --> SWISS
5427 currentState.g3Charset = CharacterSet.NRC_SWISS;
5428 }
5429 }
5430 break;
5431 case '>':
5432 case '?':
5433 case '@':
5434 break;
5435 case 'A':
bd8d51fa
KL
5436 if ((collectBuffer.length() == 1)
5437 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5438 // G0 --> United Kingdom set
5439 currentState.g0Charset = CharacterSet.UK;
5440 }
bd8d51fa
KL
5441 if ((collectBuffer.length() == 1)
5442 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5443 // G1 --> United Kingdom set
5444 currentState.g1Charset = CharacterSet.UK;
5445 }
5446 if ((type == DeviceType.VT220)
5447 || (type == DeviceType.XTERM)) {
5448
bd8d51fa
KL
5449 if ((collectBuffer.length() == 1)
5450 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5451 // G2 --> United Kingdom set
5452 currentState.g2Charset = CharacterSet.UK;
5453 }
bd8d51fa
KL
5454 if ((collectBuffer.length() == 1)
5455 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5456 // G3 --> United Kingdom set
5457 currentState.g3Charset = CharacterSet.UK;
5458 }
5459 }
5460 break;
5461 case 'B':
bd8d51fa
KL
5462 if ((collectBuffer.length() == 1)
5463 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5464 // G0 --> ASCII set
5465 currentState.g0Charset = CharacterSet.US;
5466 }
bd8d51fa
KL
5467 if ((collectBuffer.length() == 1)
5468 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5469 // G1 --> ASCII set
5470 currentState.g1Charset = CharacterSet.US;
5471 }
5472 if ((type == DeviceType.VT220)
5473 || (type == DeviceType.XTERM)) {
5474
bd8d51fa
KL
5475 if ((collectBuffer.length() == 1)
5476 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5477 // G2 --> ASCII
5478 currentState.g2Charset = CharacterSet.US;
5479 }
bd8d51fa
KL
5480 if ((collectBuffer.length() == 1)
5481 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5482 // G3 --> ASCII
5483 currentState.g3Charset = CharacterSet.US;
5484 }
5485 }
5486 break;
5487 case 'C':
5488 if ((type == DeviceType.VT220)
5489 || (type == DeviceType.XTERM)) {
5490
bd8d51fa
KL
5491 if ((collectBuffer.length() == 1)
5492 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5493 // G0 --> FINNISH
5494 currentState.g0Charset = CharacterSet.NRC_FINNISH;
5495 }
bd8d51fa
KL
5496 if ((collectBuffer.length() == 1)
5497 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5498 // G1 --> FINNISH
5499 currentState.g1Charset = CharacterSet.NRC_FINNISH;
5500 }
bd8d51fa
KL
5501 if ((collectBuffer.length() == 1)
5502 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5503 // G2 --> FINNISH
5504 currentState.g2Charset = CharacterSet.NRC_FINNISH;
5505 }
bd8d51fa
KL
5506 if ((collectBuffer.length() == 1)
5507 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5508 // G3 --> FINNISH
5509 currentState.g3Charset = CharacterSet.NRC_FINNISH;
5510 }
5511 }
5512 break;
5513 case 'D':
5514 break;
5515 case 'E':
5516 if ((type == DeviceType.VT220)
5517 || (type == DeviceType.XTERM)) {
5518
bd8d51fa
KL
5519 if ((collectBuffer.length() == 1)
5520 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5521 // G0 --> NORWEGIAN
5522 currentState.g0Charset = CharacterSet.NRC_NORWEGIAN;
5523 }
bd8d51fa
KL
5524 if ((collectBuffer.length() == 1)
5525 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5526 // G1 --> NORWEGIAN
5527 currentState.g1Charset = CharacterSet.NRC_NORWEGIAN;
5528 }
bd8d51fa
KL
5529 if ((collectBuffer.length() == 1)
5530 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5531 // G2 --> NORWEGIAN
5532 currentState.g2Charset = CharacterSet.NRC_NORWEGIAN;
5533 }
bd8d51fa
KL
5534 if ((collectBuffer.length() == 1)
5535 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5536 // G3 --> NORWEGIAN
5537 currentState.g3Charset = CharacterSet.NRC_NORWEGIAN;
5538 }
5539 }
5540 break;
5541 case 'F':
5542 if ((type == DeviceType.VT220)
5543 || (type == DeviceType.XTERM)) {
5544
bd8d51fa
KL
5545 if ((collectBuffer.length() == 1)
5546 && (collectBuffer.charAt(0) == ' ')) {
34a42e78
KL
5547 // S7C1T
5548 s8c1t = false;
5549 }
5550 }
5551 break;
5552 case 'G':
5553 if ((type == DeviceType.VT220)
5554 || (type == DeviceType.XTERM)) {
5555
bd8d51fa
KL
5556 if ((collectBuffer.length() == 1)
5557 && (collectBuffer.charAt(0) == ' ')) {
34a42e78
KL
5558 // S8C1T
5559 s8c1t = true;
5560 }
5561 }
5562 break;
5563 case 'H':
5564 if ((type == DeviceType.VT220)
5565 || (type == DeviceType.XTERM)) {
5566
bd8d51fa
KL
5567 if ((collectBuffer.length() == 1)
5568 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5569 // G0 --> SWEDISH
5570 currentState.g0Charset = CharacterSet.NRC_SWEDISH;
5571 }
bd8d51fa
KL
5572 if ((collectBuffer.length() == 1)
5573 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5574 // G1 --> SWEDISH
5575 currentState.g1Charset = CharacterSet.NRC_SWEDISH;
5576 }
bd8d51fa
KL
5577 if ((collectBuffer.length() == 1)
5578 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5579 // G2 --> SWEDISH
5580 currentState.g2Charset = CharacterSet.NRC_SWEDISH;
5581 }
bd8d51fa
KL
5582 if ((collectBuffer.length() == 1)
5583 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5584 // G3 --> SWEDISH
5585 currentState.g3Charset = CharacterSet.NRC_SWEDISH;
5586 }
5587 }
5588 break;
5589 case 'I':
5590 case 'J':
5591 break;
5592 case 'K':
5593 if ((type == DeviceType.VT220)
5594 || (type == DeviceType.XTERM)) {
5595
bd8d51fa
KL
5596 if ((collectBuffer.length() == 1)
5597 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5598 // G0 --> GERMAN
5599 currentState.g0Charset = CharacterSet.NRC_GERMAN;
5600 }
bd8d51fa
KL
5601 if ((collectBuffer.length() == 1)
5602 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5603 // G1 --> GERMAN
5604 currentState.g1Charset = CharacterSet.NRC_GERMAN;
5605 }
bd8d51fa
KL
5606 if ((collectBuffer.length() == 1)
5607 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5608 // G2 --> GERMAN
5609 currentState.g2Charset = CharacterSet.NRC_GERMAN;
5610 }
bd8d51fa
KL
5611 if ((collectBuffer.length() == 1)
5612 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5613 // G3 --> GERMAN
5614 currentState.g3Charset = CharacterSet.NRC_GERMAN;
5615 }
5616 }
5617 break;
5618 case 'L':
5619 case 'M':
5620 case 'N':
5621 case 'O':
5622 case 'P':
5623 break;
5624 case 'Q':
5625 if ((type == DeviceType.VT220)
5626 || (type == DeviceType.XTERM)) {
5627
bd8d51fa
KL
5628 if ((collectBuffer.length() == 1)
5629 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5630 // G0 --> FRENCH_CA
5631 currentState.g0Charset = CharacterSet.NRC_FRENCH_CA;
5632 }
bd8d51fa
KL
5633 if ((collectBuffer.length() == 1)
5634 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5635 // G1 --> FRENCH_CA
5636 currentState.g1Charset = CharacterSet.NRC_FRENCH_CA;
5637 }
bd8d51fa
KL
5638 if ((collectBuffer.length() == 1)
5639 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5640 // G2 --> FRENCH_CA
5641 currentState.g2Charset = CharacterSet.NRC_FRENCH_CA;
5642 }
bd8d51fa
KL
5643 if ((collectBuffer.length() == 1)
5644 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5645 // G3 --> FRENCH_CA
5646 currentState.g3Charset = CharacterSet.NRC_FRENCH_CA;
5647 }
5648 }
5649 break;
5650 case 'R':
5651 if ((type == DeviceType.VT220)
5652 || (type == DeviceType.XTERM)) {
5653
bd8d51fa
KL
5654 if ((collectBuffer.length() == 1)
5655 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5656 // G0 --> FRENCH
5657 currentState.g0Charset = CharacterSet.NRC_FRENCH;
5658 }
bd8d51fa
KL
5659 if ((collectBuffer.length() == 1)
5660 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5661 // G1 --> FRENCH
5662 currentState.g1Charset = CharacterSet.NRC_FRENCH;
5663 }
bd8d51fa
KL
5664 if ((collectBuffer.length() == 1)
5665 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5666 // G2 --> FRENCH
5667 currentState.g2Charset = CharacterSet.NRC_FRENCH;
5668 }
bd8d51fa
KL
5669 if ((collectBuffer.length() == 1)
5670 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5671 // G3 --> FRENCH
5672 currentState.g3Charset = CharacterSet.NRC_FRENCH;
5673 }
5674 }
5675 break;
5676 case 'S':
5677 case 'T':
5678 case 'U':
5679 case 'V':
5680 case 'W':
5681 case 'X':
5682 break;
5683 case 'Y':
5684 if ((type == DeviceType.VT220)
5685 || (type == DeviceType.XTERM)) {
5686
bd8d51fa
KL
5687 if ((collectBuffer.length() == 1)
5688 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5689 // G0 --> ITALIAN
5690 currentState.g0Charset = CharacterSet.NRC_ITALIAN;
5691 }
bd8d51fa
KL
5692 if ((collectBuffer.length() == 1)
5693 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5694 // G1 --> ITALIAN
5695 currentState.g1Charset = CharacterSet.NRC_ITALIAN;
5696 }
bd8d51fa
KL
5697 if ((collectBuffer.length() == 1)
5698 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5699 // G2 --> ITALIAN
5700 currentState.g2Charset = CharacterSet.NRC_ITALIAN;
5701 }
bd8d51fa
KL
5702 if ((collectBuffer.length() == 1)
5703 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5704 // G3 --> ITALIAN
5705 currentState.g3Charset = CharacterSet.NRC_ITALIAN;
5706 }
5707 }
5708 break;
5709 case 'Z':
5710 if ((type == DeviceType.VT220)
5711 || (type == DeviceType.XTERM)) {
5712
bd8d51fa
KL
5713 if ((collectBuffer.length() == 1)
5714 && (collectBuffer.charAt(0) == '(')) {
34a42e78
KL
5715 // G0 --> SPANISH
5716 currentState.g0Charset = CharacterSet.NRC_SPANISH;
5717 }
bd8d51fa
KL
5718 if ((collectBuffer.length() == 1)
5719 && (collectBuffer.charAt(0) == ')')) {
34a42e78
KL
5720 // G1 --> SPANISH
5721 currentState.g1Charset = CharacterSet.NRC_SPANISH;
5722 }
bd8d51fa
KL
5723 if ((collectBuffer.length() == 1)
5724 && (collectBuffer.charAt(0) == '*')) {
34a42e78
KL
5725 // G2 --> SPANISH
5726 currentState.g2Charset = CharacterSet.NRC_SPANISH;
5727 }
bd8d51fa
KL
5728 if ((collectBuffer.length() == 1)
5729 && (collectBuffer.charAt(0) == '+')) {
34a42e78
KL
5730 // G3 --> SPANISH
5731 currentState.g3Charset = CharacterSet.NRC_SPANISH;
5732 }
5733 }
5734 break;
5735 case '[':
5736 case '\\':
5737 case ']':
5738 case '^':
5739 case '_':
5740 case '`':
5741 case 'a':
5742 case 'b':
5743 case 'c':
5744 case 'd':
5745 case 'e':
5746 case 'f':
5747 case 'g':
5748 case 'h':
5749 case 'i':
5750 case 'j':
5751 case 'k':
5752 case 'l':
5753 case 'm':
5754 case 'n':
5755 case 'o':
5756 case 'p':
5757 case 'q':
5758 case 'r':
5759 case 's':
5760 case 't':
5761 case 'u':
5762 case 'v':
5763 case 'w':
5764 case 'x':
5765 case 'y':
5766 case 'z':
5767 case '{':
5768 case '|':
5769 case '}':
5770 case '~':
5771 break;
5772 }
5773 toGround();
5774 }
5775
5776 // 7F --> ignore
5777
5778 // 0x9C goes to GROUND
5779 if (ch == 0x9C) {
5780 toGround();
5781 }
5782
5783 return;
5784
5785 case CSI_ENTRY:
5786 // 00-17, 19, 1C-1F --> execute
5787 if (ch <= 0x1F) {
5788 handleControlChar(ch);
5789 }
5790
5791 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
5792 if ((ch >= 0x20) && (ch <= 0x2F)) {
5793 collect(ch);
5794 scanState = ScanState.CSI_INTERMEDIATE;
5795 }
5796
5797 // 30-39, 3B --> param, then switch to CSI_PARAM
5798 if ((ch >= '0') && (ch <= '9')) {
5799 param((byte) ch);
5800 scanState = ScanState.CSI_PARAM;
5801 }
5802 if (ch == ';') {
5803 param((byte) ch);
5804 scanState = ScanState.CSI_PARAM;
5805 }
5806
5807 // 3C-3F --> collect, then switch to CSI_PARAM
5808 if ((ch >= 0x3C) && (ch <= 0x3F)) {
5809 collect(ch);
5810 scanState = ScanState.CSI_PARAM;
5811 }
5812
5813 // 40-7E --> dispatch, then switch to GROUND
5814 if ((ch >= 0x40) && (ch <= 0x7E)) {
5815 switch (ch) {
5816 case '@':
5817 // ICH - Insert character
5818 ich();
5819 break;
5820 case 'A':
5821 // CUU - Cursor up
5822 cuu();
5823 break;
5824 case 'B':
5825 // CUD - Cursor down
5826 cud();
5827 break;
5828 case 'C':
5829 // CUF - Cursor forward
5830 cuf();
5831 break;
5832 case 'D':
5833 // CUB - Cursor backward
5834 cub();
5835 break;
5836 case 'E':
5837 // CNL - Cursor down and to column 1
5838 if (type == DeviceType.XTERM) {
5839 cnl();
5840 }
5841 break;
5842 case 'F':
5843 // CPL - Cursor up and to column 1
5844 if (type == DeviceType.XTERM) {
5845 cpl();
5846 }
5847 break;
5848 case 'G':
5849 // CHA - Cursor to column # in current row
5850 if (type == DeviceType.XTERM) {
5851 cha();
5852 }
5853 break;
5854 case 'H':
5855 // CUP - Cursor position
5856 cup();
5857 break;
5858 case 'I':
5859 // CHT - Cursor forward X tab stops (default 1)
5860 if (type == DeviceType.XTERM) {
5861 cht();
5862 }
5863 break;
5864 case 'J':
5865 // ED - Erase in display
5866 ed();
5867 break;
5868 case 'K':
5869 // EL - Erase in line
5870 el();
5871 break;
5872 case 'L':
5873 // IL - Insert line
5874 il();
5875 break;
5876 case 'M':
5877 // DL - Delete line
5878 dl();
5879 break;
5880 case 'N':
5881 case 'O':
5882 break;
5883 case 'P':
5884 // DCH - Delete character
5885 dch();
5886 break;
5887 case 'Q':
5888 case 'R':
5889 break;
5890 case 'S':
5891 // Scroll up X lines (default 1)
5892 if (type == DeviceType.XTERM) {
5893 su();
5894 }
5895 break;
5896 case 'T':
5897 // Scroll down X lines (default 1)
5898 if (type == DeviceType.XTERM) {
5899 sd();
5900 }
5901 break;
5902 case 'U':
5903 case 'V':
5904 case 'W':
5905 break;
5906 case 'X':
5907 if ((type == DeviceType.VT220)
5908 || (type == DeviceType.XTERM)) {
5909
5910 // ECH - Erase character
5911 ech();
5912 }
5913 break;
5914 case 'Y':
5915 break;
5916 case 'Z':
5917 // CBT - Cursor backward X tab stops (default 1)
5918 if (type == DeviceType.XTERM) {
5919 cbt();
5920 }
5921 break;
5922 case '[':
5923 case '\\':
5924 case ']':
5925 case '^':
5926 case '_':
5927 break;
5928 case '`':
5929 // HPA - Cursor to column # in current row. Same as CHA
5930 if (type == DeviceType.XTERM) {
5931 cha();
5932 }
5933 break;
5934 case 'a':
5935 // HPR - Cursor right. Same as CUF
5936 if (type == DeviceType.XTERM) {
5937 cuf();
5938 }
5939 break;
5940 case 'b':
5941 // REP - Repeat last char X times
5942 if (type == DeviceType.XTERM) {
5943 rep();
5944 }
5945 break;
5946 case 'c':
5947 // DA - Device attributes
5948 da();
5949 break;
5950 case 'd':
5951 // VPA - Cursor to row, current column.
5952 if (type == DeviceType.XTERM) {
5953 vpa();
5954 }
5955 break;
5956 case 'e':
5957 // VPR - Cursor down. Same as CUD
5958 if (type == DeviceType.XTERM) {
5959 cud();
5960 }
5961 break;
5962 case 'f':
5963 // HVP - Horizontal and vertical position
5964 hvp();
5965 break;
5966 case 'g':
5967 // TBC - Tabulation clear
5968 tbc();
5969 break;
5970 case 'h':
5971 // Sets an ANSI or DEC private toggle
5972 setToggle(true);
5973 break;
5974 case 'i':
5975 if ((type == DeviceType.VT220)
5976 || (type == DeviceType.XTERM)) {
5977
5978 // Printer functions
5979 printerFunctions();
5980 }
5981 break;
5982 case 'j':
5983 case 'k':
5984 break;
5985 case 'l':
5986 // Sets an ANSI or DEC private toggle
5987 setToggle(false);
5988 break;
5989 case 'm':
5990 // SGR - Select graphics rendition
5991 sgr();
5992 break;
5993 case 'n':
5994 // DSR - Device status report
5995 dsr();
5996 break;
5997 case 'o':
5998 case 'p':
5999 break;
6000 case 'q':
6001 // DECLL - Load leds
6002 // Not supported
6003 break;
6004 case 'r':
6005 // DECSTBM - Set top and bottom margins
6006 decstbm();
6007 break;
6008 case 's':
6009 // Save cursor (ANSI.SYS)
6010 if (type == DeviceType.XTERM) {
6011 savedState.cursorX = currentState.cursorX;
6012 savedState.cursorY = currentState.cursorY;
6013 }
6014 break;
6015 case 't':
69a8c368
KL
6016 if (type == DeviceType.XTERM) {
6017 // Window operations
6018 xtermWindowOps();
6019 }
34a42e78
KL
6020 break;
6021 case 'u':
6022 // Restore cursor (ANSI.SYS)
6023 if (type == DeviceType.XTERM) {
6024 cursorPosition(savedState.cursorY, savedState.cursorX);
6025 }
6026 break;
6027 case 'v':
6028 case 'w':
6029 break;
6030 case 'x':
6031 // DECREQTPARM - Request terminal parameters
6032 decreqtparm();
6033 break;
6034 case 'y':
6035 case 'z':
6036 case '{':
6037 case '|':
6038 case '}':
6039 case '~':
6040 break;
6041 }
6042 toGround();
6043 }
6044
6045 // 7F --> ignore
6046
6047 // 0x9C goes to GROUND
6048 if (ch == 0x9C) {
6049 toGround();
6050 }
6051
6052 // 0x3A goes to CSI_IGNORE
6053 if (ch == 0x3A) {
6054 scanState = ScanState.CSI_IGNORE;
6055 }
6056 return;
6057
6058 case CSI_PARAM:
6059 // 00-17, 19, 1C-1F --> execute
6060 if (ch <= 0x1F) {
6061 handleControlChar(ch);
6062 }
6063
6064 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
6065 if ((ch >= 0x20) && (ch <= 0x2F)) {
6066 collect(ch);
6067 scanState = ScanState.CSI_INTERMEDIATE;
6068 }
6069
6070 // 30-39, 3B --> param
6071 if ((ch >= '0') && (ch <= '9')) {
6072 param((byte) ch);
6073 }
6074 if (ch == ';') {
6075 param((byte) ch);
6076 }
6077
6078 // 0x3A goes to CSI_IGNORE
6079 if (ch == 0x3A) {
6080 scanState = ScanState.CSI_IGNORE;
6081 }
6082 // 0x3C-3F goes to CSI_IGNORE
6083 if ((ch >= 0x3C) && (ch <= 0x3F)) {
6084 scanState = ScanState.CSI_IGNORE;
6085 }
6086
6087 // 40-7E --> dispatch, then switch to GROUND
6088 if ((ch >= 0x40) && (ch <= 0x7E)) {
6089 switch (ch) {
6090 case '@':
6091 // ICH - Insert character
6092 ich();
6093 break;
6094 case 'A':
6095 // CUU - Cursor up
6096 cuu();
6097 break;
6098 case 'B':
6099 // CUD - Cursor down
6100 cud();
6101 break;
6102 case 'C':
6103 // CUF - Cursor forward
6104 cuf();
6105 break;
6106 case 'D':
6107 // CUB - Cursor backward
6108 cub();
6109 break;
6110 case 'E':
6111 // CNL - Cursor down and to column 1
6112 if (type == DeviceType.XTERM) {
6113 cnl();
6114 }
6115 break;
6116 case 'F':
6117 // CPL - Cursor up and to column 1
6118 if (type == DeviceType.XTERM) {
6119 cpl();
6120 }
6121 break;
6122 case 'G':
6123 // CHA - Cursor to column # in current row
6124 if (type == DeviceType.XTERM) {
6125 cha();
6126 }
6127 break;
6128 case 'H':
6129 // CUP - Cursor position
6130 cup();
6131 break;
6132 case 'I':
6133 // CHT - Cursor forward X tab stops (default 1)
6134 if (type == DeviceType.XTERM) {
6135 cht();
6136 }
6137 break;
6138 case 'J':
6139 // ED - Erase in display
6140 ed();
6141 break;
6142 case 'K':
6143 // EL - Erase in line
6144 el();
6145 break;
6146 case 'L':
6147 // IL - Insert line
6148 il();
6149 break;
6150 case 'M':
6151 // DL - Delete line
6152 dl();
6153 break;
6154 case 'N':
6155 case 'O':
6156 break;
6157 case 'P':
6158 // DCH - Delete character
6159 dch();
6160 break;
6161 case 'Q':
6162 case 'R':
6163 break;
6164 case 'S':
6165 // Scroll up X lines (default 1)
6166 if (type == DeviceType.XTERM) {
6167 su();
6168 }
6169 break;
6170 case 'T':
6171 // Scroll down X lines (default 1)
6172 if (type == DeviceType.XTERM) {
6173 sd();
6174 }
6175 break;
6176 case 'U':
6177 case 'V':
6178 case 'W':
6179 break;
6180 case 'X':
6181 if ((type == DeviceType.VT220)
6182 || (type == DeviceType.XTERM)) {
6183
6184 // ECH - Erase character
6185 ech();
6186 }
6187 break;
6188 case 'Y':
6189 break;
6190 case 'Z':
6191 // CBT - Cursor backward X tab stops (default 1)
6192 if (type == DeviceType.XTERM) {
6193 cbt();
6194 }
6195 break;
6196 case '[':
6197 case '\\':
6198 case ']':
6199 case '^':
6200 case '_':
6201 break;
6202 case '`':
6203 // HPA - Cursor to column # in current row. Same as CHA
6204 if (type == DeviceType.XTERM) {
6205 cha();
6206 }
6207 break;
6208 case 'a':
6209 // HPR - Cursor right. Same as CUF
6210 if (type == DeviceType.XTERM) {
6211 cuf();
6212 }
6213 break;
6214 case 'b':
6215 // REP - Repeat last char X times
6216 if (type == DeviceType.XTERM) {
6217 rep();
6218 }
6219 break;
6220 case 'c':
6221 // DA - Device attributes
6222 da();
6223 break;
6224 case 'd':
6225 // VPA - Cursor to row, current column.
6226 if (type == DeviceType.XTERM) {
6227 vpa();
6228 }
6229 break;
6230 case 'e':
6231 // VPR - Cursor down. Same as CUD
6232 if (type == DeviceType.XTERM) {
6233 cud();
6234 }
6235 break;
6236 case 'f':
6237 // HVP - Horizontal and vertical position
6238 hvp();
6239 break;
6240 case 'g':
6241 // TBC - Tabulation clear
6242 tbc();
6243 break;
6244 case 'h':
6245 // Sets an ANSI or DEC private toggle
6246 setToggle(true);
6247 break;
6248 case 'i':
6249 if ((type == DeviceType.VT220)
6250 || (type == DeviceType.XTERM)) {
6251
6252 // Printer functions
6253 printerFunctions();
6254 }
6255 break;
6256 case 'j':
6257 case 'k':
6258 break;
6259 case 'l':
6260 // Sets an ANSI or DEC private toggle
6261 setToggle(false);
6262 break;
6263 case 'm':
6264 // SGR - Select graphics rendition
6265 sgr();
6266 break;
6267 case 'n':
6268 // DSR - Device status report
6269 dsr();
6270 break;
6271 case 'o':
6272 case 'p':
6273 break;
6274 case 'q':
6275 // DECLL - Load leds
6276 // Not supported
6277 break;
6278 case 'r':
6279 // DECSTBM - Set top and bottom margins
6280 decstbm();
6281 break;
6282 case 's':
69a8c368 6283 break;
34a42e78 6284 case 't':
69a8c368
KL
6285 if (type == DeviceType.XTERM) {
6286 // Window operations
6287 xtermWindowOps();
6288 }
6289 break;
34a42e78
KL
6290 case 'u':
6291 case 'v':
6292 case 'w':
6293 break;
6294 case 'x':
6295 // DECREQTPARM - Request terminal parameters
6296 decreqtparm();
6297 break;
6298 case 'y':
6299 case 'z':
6300 case '{':
6301 case '|':
6302 case '}':
6303 case '~':
6304 break;
6305 }
6306 toGround();
6307 }
6308
6309 // 7F --> ignore
6310 return;
6311
6312 case CSI_INTERMEDIATE:
6313 // 00-17, 19, 1C-1F --> execute
6314 if (ch <= 0x1F) {
6315 handleControlChar(ch);
6316 }
6317
6318 // 20-2F --> collect
6319 if ((ch >= 0x20) && (ch <= 0x2F)) {
6320 collect(ch);
6321 }
6322
6323 // 0x30-3F goes to CSI_IGNORE
6324 if ((ch >= 0x30) && (ch <= 0x3F)) {
6325 scanState = ScanState.CSI_IGNORE;
6326 }
6327
6328 // 40-7E --> dispatch, then switch to GROUND
6329 if ((ch >= 0x40) && (ch <= 0x7E)) {
6330 switch (ch) {
6331 case '@':
6332 case 'A':
6333 case 'B':
6334 case 'C':
6335 case 'D':
6336 case 'E':
6337 case 'F':
6338 case 'G':
6339 case 'H':
6340 case 'I':
6341 case 'J':
6342 case 'K':
6343 case 'L':
6344 case 'M':
6345 case 'N':
6346 case 'O':
6347 case 'P':
6348 case 'Q':
6349 case 'R':
6350 case 'S':
6351 case 'T':
6352 case 'U':
6353 case 'V':
6354 case 'W':
6355 case 'X':
6356 case 'Y':
6357 case 'Z':
6358 case '[':
6359 case '\\':
6360 case ']':
6361 case '^':
6362 case '_':
6363 case '`':
6364 case 'a':
6365 case 'b':
6366 case 'c':
6367 case 'd':
6368 case 'e':
6369 case 'f':
6370 case 'g':
6371 case 'h':
6372 case 'i':
6373 case 'j':
6374 case 'k':
6375 case 'l':
6376 case 'm':
6377 case 'n':
6378 case 'o':
6379 break;
6380 case 'p':
6381 if (((type == DeviceType.VT220)
6382 || (type == DeviceType.XTERM))
bd8d51fa 6383 && (collectBuffer.charAt(collectBuffer.length() - 1) == '\"')
34a42e78
KL
6384 ) {
6385 // DECSCL - compatibility level
6386 decscl();
6387 }
6388 if ((type == DeviceType.XTERM)
bd8d51fa 6389 && (collectBuffer.charAt(collectBuffer.length() - 1) == '!')
34a42e78
KL
6390 ) {
6391 // DECSTR - Soft terminal reset
6392 decstr();
6393 }
6394 break;
6395 case 'q':
6396 if (((type == DeviceType.VT220)
6397 || (type == DeviceType.XTERM))
bd8d51fa 6398 && (collectBuffer.charAt(collectBuffer.length() - 1) == '\"')
34a42e78
KL
6399 ) {
6400 // DECSCA
6401 decsca();
6402 }
6403 break;
6404 case 'r':
6405 case 's':
6406 case 't':
6407 case 'u':
6408 case 'v':
6409 case 'w':
6410 case 'x':
6411 case 'y':
6412 case 'z':
6413 case '{':
6414 case '|':
6415 case '}':
6416 case '~':
6417 break;
6418 }
6419 toGround();
6420 }
6421
6422 // 7F --> ignore
6423 return;
6424
6425 case CSI_IGNORE:
6426 // 00-17, 19, 1C-1F --> execute
6427 if (ch <= 0x1F) {
6428 handleControlChar(ch);
6429 }
6430
6431 // 20-2F --> collect
6432 if ((ch >= 0x20) && (ch <= 0x2F)) {
6433 collect(ch);
6434 }
6435
6436 // 40-7E --> ignore, then switch to GROUND
6437 if ((ch >= 0x40) && (ch <= 0x7E)) {
6438 toGround();
6439 }
6440
6441 // 20-3F, 7F --> ignore
6442
6443 return;
6444
6445 case DCS_ENTRY:
6446
6447 // 0x9C goes to GROUND
6448 if (ch == 0x9C) {
6449 toGround();
6450 }
6451
6452 // 0x1B 0x5C goes to GROUND
6453 if (ch == 0x1B) {
6454 collect(ch);
6455 }
6456 if (ch == 0x5C) {
bd8d51fa
KL
6457 if ((collectBuffer.length() > 0)
6458 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
6459 ) {
34a42e78
KL
6460 toGround();
6461 }
6462 }
6463
6464 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6465 if ((ch >= 0x20) && (ch <= 0x2F)) {
6466 collect(ch);
6467 scanState = ScanState.DCS_INTERMEDIATE;
6468 }
6469
6470 // 30-39, 3B --> param, then switch to DCS_PARAM
6471 if ((ch >= '0') && (ch <= '9')) {
6472 param((byte) ch);
6473 scanState = ScanState.DCS_PARAM;
6474 }
6475 if (ch == ';') {
6476 param((byte) ch);
6477 scanState = ScanState.DCS_PARAM;
6478 }
6479
6480 // 3C-3F --> collect, then switch to DCS_PARAM
6481 if ((ch >= 0x3C) && (ch <= 0x3F)) {
6482 collect(ch);
6483 scanState = ScanState.DCS_PARAM;
6484 }
6485
6486 // 00-17, 19, 1C-1F, 7F --> ignore
6487
6488 // 0x3A goes to DCS_IGNORE
6489 if (ch == 0x3F) {
6490 scanState = ScanState.DCS_IGNORE;
6491 }
6492
5fc7bf09
KL
6493 // 0x71 goes to DCS_SIXEL
6494 if (ch == 0x71) {
6495 sixelParseBuffer = new StringBuilder();
6496 scanState = ScanState.DCS_SIXEL;
6497 } else if ((ch >= 0x40) && (ch <= 0x7E)) {
6498 // 0x40-7E goes to DCS_PASSTHROUGH
34a42e78
KL
6499 scanState = ScanState.DCS_PASSTHROUGH;
6500 }
6501 return;
6502
6503 case DCS_INTERMEDIATE:
6504
6505 // 0x9C goes to GROUND
6506 if (ch == 0x9C) {
6507 toGround();
6508 }
6509
6510 // 0x1B 0x5C goes to GROUND
6511 if (ch == 0x1B) {
6512 collect(ch);
6513 }
6514 if (ch == 0x5C) {
329fd62e
KL
6515 if ((collectBuffer.length() > 0)
6516 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
bd8d51fa 6517 ) {
34a42e78
KL
6518 toGround();
6519 }
6520 }
6521
6522 // 0x30-3F goes to DCS_IGNORE
6523 if ((ch >= 0x30) && (ch <= 0x3F)) {
6524 scanState = ScanState.DCS_IGNORE;
6525 }
6526
6527 // 0x40-7E goes to DCS_PASSTHROUGH
6528 if ((ch >= 0x40) && (ch <= 0x7E)) {
6529 scanState = ScanState.DCS_PASSTHROUGH;
6530 }
6531
6532 // 00-17, 19, 1C-1F, 7F --> ignore
6533 return;
6534
6535 case DCS_PARAM:
6536
6537 // 0x9C goes to GROUND
6538 if (ch == 0x9C) {
6539 toGround();
6540 }
6541
6542 // 0x1B 0x5C goes to GROUND
6543 if (ch == 0x1B) {
6544 collect(ch);
6545 }
6546 if (ch == 0x5C) {
329fd62e
KL
6547 if ((collectBuffer.length() > 0)
6548 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
bd8d51fa 6549 ) {
34a42e78
KL
6550 toGround();
6551 }
6552 }
6553
6554 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
6555 if ((ch >= 0x20) && (ch <= 0x2F)) {
6556 collect(ch);
6557 scanState = ScanState.DCS_INTERMEDIATE;
6558 }
6559
6560 // 30-39, 3B --> param
6561 if ((ch >= '0') && (ch <= '9')) {
6562 param((byte) ch);
6563 }
6564 if (ch == ';') {
6565 param((byte) ch);
6566 }
6567
6568 // 00-17, 19, 1C-1F, 7F --> ignore
6569
6570 // 0x3A, 3C-3F goes to DCS_IGNORE
6571 if (ch == 0x3F) {
6572 scanState = ScanState.DCS_IGNORE;
6573 }
6574 if ((ch >= 0x3C) && (ch <= 0x3F)) {
6575 scanState = ScanState.DCS_IGNORE;
6576 }
6577
5fc7bf09
KL
6578 // 0x71 goes to DCS_SIXEL
6579 if (ch == 0x71) {
6580 sixelParseBuffer = new StringBuilder();
6581 scanState = ScanState.DCS_SIXEL;
6582 } else if ((ch >= 0x40) && (ch <= 0x7E)) {
6583 // 0x40-7E goes to DCS_PASSTHROUGH
34a42e78
KL
6584 scanState = ScanState.DCS_PASSTHROUGH;
6585 }
6586 return;
6587
6588 case DCS_PASSTHROUGH:
6589 // 0x9C goes to GROUND
6590 if (ch == 0x9C) {
6591 toGround();
6592 }
6593
6594 // 0x1B 0x5C goes to GROUND
6595 if (ch == 0x1B) {
6596 collect(ch);
6597 }
6598 if (ch == 0x5C) {
bd8d51fa
KL
6599 if ((collectBuffer.length() > 0)
6600 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
34a42e78
KL
6601 ) {
6602 toGround();
6603 }
6604 }
6605
6606 // 00-17, 19, 1C-1F, 20-7E --> put
6607 // TODO
6608 if (ch <= 0x17) {
6609 return;
6610 }
6611 if (ch == 0x19) {
6612 return;
6613 }
6614 if ((ch >= 0x1C) && (ch <= 0x1F)) {
6615 return;
6616 }
6617 if ((ch >= 0x20) && (ch <= 0x7E)) {
6618 return;
6619 }
6620
6621 // 7F --> ignore
6622
6623 return;
6624
6625 case DCS_IGNORE:
6626 // 00-17, 19, 1C-1F, 20-7F --> ignore
6627
6628 // 0x9C goes to GROUND
6629 if (ch == 0x9C) {
6630 toGround();
6631 }
6632
6633 return;
6634
5fc7bf09
KL
6635 case DCS_SIXEL:
6636 // 0x9C goes to GROUND
6637 if (ch == 0x9C) {
6638 parseSixel();
6639 toGround();
6640 }
6641
6642 // 0x1B 0x5C goes to GROUND
6643 if (ch == 0x1B) {
6644 collect(ch);
6645 }
6646 if (ch == 0x5C) {
6647 if ((collectBuffer.length() > 0)
6648 && (collectBuffer.charAt(collectBuffer.length() - 1) == 0x1B)
6649 ) {
6650 parseSixel();
6651 toGround();
6652 }
6653 }
6654
6655 // 00-17, 19, 1C-1F, 20-7E --> put
6656 if (ch <= 0x17) {
6657 sixelParseBuffer.append(ch);
6658 return;
6659 }
6660 if (ch == 0x19) {
6661 sixelParseBuffer.append(ch);
6662 return;
6663 }
6664 if ((ch >= 0x1C) && (ch <= 0x1F)) {
6665 sixelParseBuffer.append(ch);
6666 return;
6667 }
6668 if ((ch >= 0x20) && (ch <= 0x7E)) {
6669 sixelParseBuffer.append(ch);
6670 return;
6671 }
6672
6673 // 7F --> ignore
6674
6675 return;
6676
34a42e78
KL
6677 case SOSPMAPC_STRING:
6678 // 00-17, 19, 1C-1F, 20-7F --> ignore
6679
978a5d8f
KL
6680 // Special case for Jexer: PM can pass one control character
6681 if (ch == 0x1B) {
6682 pmPut(ch);
6683 }
6684
6685 if ((ch >= 0x20) && (ch <= 0x7F)) {
6686 pmPut(ch);
6687 }
6688
34a42e78
KL
6689 // 0x9C goes to GROUND
6690 if (ch == 0x9C) {
6691 toGround();
6692 }
6693
6694 return;
6695
6696 case OSC_STRING:
6697 // Special case for Xterm: OSC can pass control characters
051e2913 6698 if ((ch == 0x9C) || (ch == 0x07) || (ch == 0x1B)) {
34a42e78
KL
6699 oscPut(ch);
6700 }
6701
6702 // 00-17, 19, 1C-1F --> ignore
6703
6704 // 20-7F --> osc_put
6705 if ((ch >= 0x20) && (ch <= 0x7F)) {
6706 oscPut(ch);
6707 }
6708
6709 // 0x9C goes to GROUND
6710 if (ch == 0x9C) {
6711 toGround();
6712 }
6713
6714 return;
6715
6716 case VT52_DIRECT_CURSOR_ADDRESS:
6717 // This is a special case for the VT52 sequence "ESC Y l c"
bd8d51fa 6718 if (collectBuffer.length() == 0) {
34a42e78 6719 collect(ch);
bd8d51fa 6720 } else if (collectBuffer.length() == 1) {
34a42e78
KL
6721 // We've got the two characters, one in the buffer and the
6722 // other in ch.
bd8d51fa 6723 cursorPosition(collectBuffer.charAt(0) - '\040', ch - '\040');
34a42e78
KL
6724 toGround();
6725 }
6726 return;
6727 }
6728
6729 }
6730
6731 /**
6732 * Expose current cursor X to outside world.
6733 *
6734 * @return current cursor X
6735 */
6736 public final int getCursorX() {
bd8d51fa
KL
6737 if (display.get(currentState.cursorY).isDoubleWidth()) {
6738 return currentState.cursorX * 2;
6739 }
34a42e78
KL
6740 return currentState.cursorX;
6741 }
6742
6743 /**
6744 * Expose current cursor Y to outside world.
6745 *
6746 * @return current cursor Y
6747 */
6748 public final int getCursorY() {
6749 return currentState.cursorY;
6750 }
6751
978a5d8f
KL
6752 /**
6753 * Returns true if this terminal has requested the mouse pointer be
6754 * hidden.
6755 *
6756 * @return true if this terminal has requested the mouse pointer be
6757 * hidden
6758 */
6759 public final boolean hasHiddenMousePointer() {
6760 return hideMousePointer;
6761 }
6762
69a8c368
KL
6763 /**
6764 * Get the mouse protocol.
6765 *
6766 * @return MouseProtocol.OFF, MouseProtocol.X10, etc.
6767 */
6768 public MouseProtocol getMouseProtocol() {
6769 return mouseProtocol;
6770 }
6771
5fc7bf09
KL
6772 // ------------------------------------------------------------------------
6773 // Sixel support ----------------------------------------------------------
6774 // ------------------------------------------------------------------------
6775
6776 /**
6777 * Set the width of a character cell in pixels.
6778 *
6779 * @param textWidth the width in pixels of a character cell
6780 */
6781 public void setTextWidth(final int textWidth) {
6782 this.textWidth = textWidth;
6783 }
6784
6785 /**
6786 * Set the height of a character cell in pixels.
6787 *
6788 * @param textHeight the height in pixels of a character cell
6789 */
6790 public void setTextHeight(final int textHeight) {
6791 this.textHeight = textHeight;
6792 }
6793
6794 /**
6795 * Parse a sixel string into a bitmap image, and overlay that image onto
6796 * the text cells.
6797 */
6798 private void parseSixel() {
69a8c368
KL
6799
6800 /*
6801 System.err.println("parseSixel(): '" + sixelParseBuffer.toString()
6802 + "'");
6803 */
5fc7bf09
KL
6804
6805 Sixel sixel = new Sixel(sixelParseBuffer.toString());
6806 BufferedImage image = sixel.getImage();
6807
69a8c368 6808 // System.err.println("parseSixel(): image " + image);
5fc7bf09
KL
6809
6810 if (image == null) {
6811 // Sixel data was malformed in some way, bail out.
6812 return;
6813 }
6814
6815 /*
6816 * Procedure:
6817 *
6818 * Break up the image into text cell sized pieces as a new array of
6819 * Cells.
6820 *
6821 * Note original column position x0.
6822 *
6823 * For each cell:
6824 *
6825 * 1. Advance (printCharacter(' ')) for horizontal increment, or
6826 * index (linefeed() + cursorPosition(y, x0)) for vertical
6827 * increment.
6828 *
6829 * 2. Set (x, y) cell image data.
6830 *
6831 * 3. For the right and bottom edges:
6832 *
6833 * a. Render the text to pixels using Terminus font.
6834 *
6835 * b. Blit the image on top of the text, using alpha channel.
6836 */
6837 int cellColumns = image.getWidth() / textWidth;
6838 if (cellColumns * textWidth < image.getWidth()) {
6839 cellColumns++;
6840 }
6841 int cellRows = image.getHeight() / textHeight;
6842 if (cellRows * textHeight < image.getHeight()) {
6843 cellRows++;
6844 }
6845
6846 // Break the image up into an array of cells.
6847 Cell [][] cells = new Cell[cellColumns][cellRows];
6848
6849 for (int x = 0; x < cellColumns; x++) {
6850 for (int y = 0; y < cellRows; y++) {
6851
6852 int width = textWidth;
6853 if ((x + 1) * textWidth > image.getWidth()) {
6854 width = image.getWidth() - (x * textWidth);
6855 }
6856 int height = textHeight;
6857 if ((y + 1) * textHeight > image.getHeight()) {
6858 height = image.getHeight() - (y * textHeight);
6859 }
6860
6861 Cell cell = new Cell();
6862 cell.setImage(image.getSubimage(x * textWidth,
6863 y * textHeight, width, height));
6864
6865 cells[x][y] = cell;
6866 }
6867 }
6868
6869 int x0 = currentState.cursorX;
6870 for (int y = 0; y < cellRows; y++) {
6871 for (int x = 0; x < cellColumns; x++) {
6872 printCharacter(' ');
6873 cursorLeft(1, false);
6874 if ((x == cellColumns - 1) || (y == cellRows - 1)) {
6875 // TODO: render text of current cell first, then image
6876 // over it. For now, just copy the cell.
6877 DisplayLine line = display.get(currentState.cursorY);
6878 line.replace(currentState.cursorX, cells[x][y]);
6879 } else {
6880 // Copy the image cell into the display.
6881 DisplayLine line = display.get(currentState.cursorY);
6882 line.replace(currentState.cursorX, cells[x][y]);
6883 }
6884 cursorRight(1, false);
6885 }
6886 linefeed();
6887 cursorPosition(currentState.cursorY, x0);
6888 }
6889
6890 }
6891
9588c713
KL
6892 /**
6893 * Draw the left and right cells of a two-cell-wide (full-width) glyph.
6894 *
6895 * @param leftX the x position to draw the left half to
6896 * @param leftY the y position to draw the left half to
6897 * @param rightX the x position to draw the right half to
6898 * @param rightY the y position to draw the right half to
6899 * @param ch the character to draw
6900 */
6901 private void drawHalves(final int leftX, final int leftY,
6902 final int rightX, final int rightY, final char ch) {
6903
6904 // System.err.println("drawHalves(): " + Integer.toHexString(ch));
6905
6906 if (lastTextHeight != textHeight) {
6907 glyphMaker = GlyphMaker.getInstance(textHeight);
6908 lastTextHeight = textHeight;
6909 }
6910
027de5ae 6911 Cell cell = new Cell(ch, currentState.attr);
9588c713
KL
6912 BufferedImage image = glyphMaker.getImage(cell, textWidth * 2,
6913 textHeight);
6914 BufferedImage leftImage = image.getSubimage(0, 0, textWidth,
6915 textHeight);
6916 BufferedImage rightImage = image.getSubimage(textWidth, 0, textWidth,
6917 textHeight);
6918
027de5ae 6919 Cell left = new Cell(cell);
9588c713
KL
6920 left.setImage(leftImage);
6921 left.setWidth(Cell.Width.LEFT);
6922 display.get(leftY).replace(leftX, left);
6923
027de5ae 6924 Cell right = new Cell(cell);
9588c713
KL
6925 right.setImage(rightImage);
6926 right.setWidth(Cell.Width.RIGHT);
6927 display.get(rightY).replace(rightX, right);
6928 }
6929
34a42e78 6930}