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