Commit | Line | Data |
---|---|---|
4d2c61b4 KL |
1 | /* |
2 | * Jexer - Java Text User Interface | |
3 | * | |
4 | * The MIT License (MIT) | |
5 | * | |
6 | * Copyright (C) 2019 Kevin Lamonte | |
7 | * | |
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: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
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. | |
25 | * | |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
31 | import java.awt.Font; | |
32 | import java.awt.FontMetrics; | |
33 | import java.awt.Graphics2D; | |
34 | import java.awt.image.BufferedImage; | |
35 | ||
36 | import java.io.InputStream; | |
37 | import java.io.IOException; | |
38 | import java.lang.reflect.Field; | |
39 | import java.text.MessageFormat; | |
40 | import java.util.ArrayList; | |
41 | import java.util.HashMap; | |
42 | import java.util.List; | |
43 | import java.util.Map; | |
44 | import java.util.ResourceBundle; | |
45 | ||
46 | import jexer.backend.ECMA48Terminal; | |
47 | import jexer.backend.GlyphMaker; | |
48 | import jexer.backend.MultiScreen; | |
49 | import jexer.backend.SwingTerminal; | |
50 | import jexer.bits.Cell; | |
51 | import jexer.bits.CellAttributes; | |
52 | import jexer.event.TKeypressEvent; | |
53 | import jexer.event.TMenuEvent; | |
54 | import jexer.event.TMouseEvent; | |
55 | import jexer.event.TResizeEvent; | |
56 | import jexer.menu.TMenu; | |
57 | import jexer.tterminal.DisplayLine; | |
58 | import jexer.tterminal.DisplayListener; | |
59 | import jexer.tterminal.ECMA48; | |
60 | import static jexer.TKeypress.*; | |
61 | ||
62 | /** | |
2bc32111 | 63 | * TTerminalWidget exposes a ECMA-48 / ANSI X3.64 style terminal in a widget. |
4d2c61b4 KL |
64 | */ |
65 | public class TTerminalWidget extends TScrollableWidget | |
66 | implements DisplayListener { | |
67 | ||
68 | /** | |
69 | * Translated strings. | |
70 | */ | |
71 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TTerminalWidget.class.getName()); | |
72 | ||
73 | // ------------------------------------------------------------------------ | |
74 | // Variables -------------------------------------------------------------- | |
75 | // ------------------------------------------------------------------------ | |
76 | ||
77 | /** | |
78 | * The emulator. | |
79 | */ | |
80 | private ECMA48 emulator; | |
81 | ||
82 | /** | |
83 | * The Process created by the shell spawning constructor. | |
84 | */ | |
85 | private Process shell; | |
86 | ||
87 | /** | |
88 | * If true, we are using the ptypipe utility to support dynamic window | |
89 | * resizing. ptypipe is available at | |
90 | * https://gitlab.com/klamonte/ptypipe . | |
91 | */ | |
92 | private boolean ptypipe = false; | |
93 | ||
94 | /** | |
95 | * Double-height font. | |
96 | */ | |
97 | private GlyphMaker doubleFont; | |
98 | ||
99 | /** | |
100 | * Last text width value. | |
101 | */ | |
102 | private int lastTextWidth = -1; | |
103 | ||
104 | /** | |
105 | * Last text height value. | |
106 | */ | |
107 | private int lastTextHeight = -1; | |
108 | ||
109 | /** | |
110 | * The blink state, used only by ECMA48 backend and when double-width | |
111 | * chars must be drawn. | |
112 | */ | |
113 | private boolean blinkState = true; | |
114 | ||
115 | /** | |
116 | * Timer flag, used only by ECMA48 backend and when double-width chars | |
117 | * must be drawn. | |
118 | */ | |
119 | private boolean haveTimer = false; | |
120 | ||
121 | /** | |
122 | * The last seen scrollback lines. | |
123 | */ | |
124 | private List<DisplayLine> scrollback; | |
125 | ||
126 | /** | |
127 | * The last seen display lines. | |
128 | */ | |
129 | private List<DisplayLine> display; | |
130 | ||
131 | /** | |
132 | * If true, the display has changed and needs updating. | |
133 | */ | |
134 | private volatile boolean dirty = true; | |
135 | ||
136 | /** | |
137 | * Time that the display was last updated. | |
138 | */ | |
139 | private long lastUpdateTime = 0; | |
140 | ||
141 | /** | |
142 | * If true, hide the mouse after typing a keystroke. | |
143 | */ | |
144 | private boolean hideMouseWhenTyping = true; | |
145 | ||
146 | /** | |
147 | * If true, the mouse should not be displayed because a keystroke was | |
148 | * typed. | |
149 | */ | |
150 | private boolean typingHidMouse = false; | |
151 | ||
152 | /** | |
153 | * The return value from the emulator. | |
154 | */ | |
155 | private int exitValue = -1; | |
156 | ||
157 | /** | |
158 | * Title to expose to a window. | |
159 | */ | |
160 | private String title = ""; | |
161 | ||
2bc32111 KL |
162 | /** |
163 | * Action to perform when the terminal exits. | |
164 | */ | |
165 | private TAction closeAction = null; | |
166 | ||
4d2c61b4 KL |
167 | // ------------------------------------------------------------------------ |
168 | // Constructors ----------------------------------------------------------- | |
169 | // ------------------------------------------------------------------------ | |
170 | ||
171 | /** | |
172 | * Public constructor spawns a custom command line. | |
173 | * | |
174 | * @param parent parent widget | |
175 | * @param x column relative to parent | |
176 | * @param y row relative to parent | |
177 | * @param commandLine the command line to execute | |
178 | */ | |
2bc32111 KL |
179 | public TTerminalWidget(final TWidget parent, final int x, final int y, |
180 | final String commandLine) { | |
4d2c61b4 KL |
181 | |
182 | this(parent, x, y, commandLine.split("\\s+")); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Public constructor spawns a custom command line. | |
187 | * | |
188 | * @param parent parent widget | |
189 | * @param x column relative to parent | |
190 | * @param y row relative to parent | |
191 | * @param command the command line to execute | |
192 | */ | |
2bc32111 KL |
193 | public TTerminalWidget(final TWidget parent, final int x, final int y, |
194 | final String [] command) { | |
195 | ||
196 | this(parent, x, y, command, null); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Public constructor spawns a custom command line. | |
201 | * | |
202 | * @param parent parent widget | |
203 | * @param x column relative to parent | |
204 | * @param y row relative to parent | |
205 | * @param command the command line to execute | |
206 | * @param closeAction action to perform when the shell sxits | |
207 | */ | |
208 | public TTerminalWidget(final TWidget parent, final int x, final int y, | |
209 | final String [] command, final TAction closeAction) { | |
210 | ||
211 | this(parent, x, y, 80, 24, command, closeAction); | |
212 | } | |
213 | ||
214 | /** | |
215 | * Public constructor spawns a custom command line. | |
216 | * | |
217 | * @param parent parent widget | |
218 | * @param x column relative to parent | |
219 | * @param y row relative to parent | |
220 | * @param width width of widget | |
221 | * @param height height of widget | |
222 | * @param command the command line to execute | |
223 | * @param closeAction action to perform when the shell sxits | |
224 | */ | |
225 | public TTerminalWidget(final TWidget parent, final int x, final int y, | |
226 | final int width, final int height, final String [] command, | |
227 | final TAction closeAction) { | |
228 | ||
229 | super(parent, x, y, width, height); | |
4d2c61b4 | 230 | |
2bc32111 | 231 | this.closeAction = closeAction; |
4d2c61b4 KL |
232 | |
233 | String [] fullCommand; | |
234 | ||
235 | // Spawn a shell and pass its I/O to the other constructor. | |
236 | if ((System.getProperty("jexer.TTerminal.ptypipe") != null) | |
237 | && (System.getProperty("jexer.TTerminal.ptypipe"). | |
238 | equals("true")) | |
239 | ) { | |
240 | ptypipe = true; | |
241 | fullCommand = new String[command.length + 1]; | |
242 | fullCommand[0] = "ptypipe"; | |
243 | System.arraycopy(command, 0, fullCommand, 1, command.length); | |
244 | } else if (System.getProperty("os.name").startsWith("Windows")) { | |
245 | fullCommand = new String[3]; | |
246 | fullCommand[0] = "cmd"; | |
247 | fullCommand[1] = "/c"; | |
248 | fullCommand[2] = stringArrayToString(command); | |
249 | } else if (System.getProperty("os.name").startsWith("Mac")) { | |
250 | fullCommand = new String[6]; | |
251 | fullCommand[0] = "script"; | |
252 | fullCommand[1] = "-q"; | |
253 | fullCommand[2] = "-F"; | |
254 | fullCommand[3] = "/dev/null"; | |
255 | fullCommand[4] = "-c"; | |
256 | fullCommand[5] = stringArrayToString(command); | |
257 | } else { | |
258 | // Default: behave like Linux | |
259 | fullCommand = new String[5]; | |
260 | fullCommand[0] = "script"; | |
261 | fullCommand[1] = "-fqe"; | |
262 | fullCommand[2] = "/dev/null"; | |
263 | fullCommand[3] = "-c"; | |
264 | fullCommand[4] = stringArrayToString(command); | |
265 | } | |
266 | spawnShell(fullCommand); | |
267 | } | |
268 | ||
269 | /** | |
270 | * Public constructor spawns a shell. | |
271 | * | |
272 | * @param parent parent widget | |
273 | * @param x column relative to parent | |
274 | * @param y row relative to parent | |
275 | */ | |
276 | public TTerminalWidget(final TWidget parent, final int x, final int y) { | |
2bc32111 KL |
277 | this(parent, x, y, (TAction) null); |
278 | } | |
4d2c61b4 | 279 | |
2bc32111 KL |
280 | /** |
281 | * Public constructor spawns a shell. | |
282 | * | |
283 | * @param parent parent widget | |
284 | * @param x column relative to parent | |
285 | * @param y row relative to parent | |
286 | * @param closeAction action to perform when the shell sxits | |
287 | */ | |
288 | public TTerminalWidget(final TWidget parent, final int x, final int y, | |
289 | final TAction closeAction) { | |
290 | ||
291 | this(parent, x, y, 80, 24, closeAction); | |
292 | } | |
293 | ||
294 | /** | |
295 | * Public constructor spawns a shell. | |
296 | * | |
297 | * @param parent parent widget | |
298 | * @param x column relative to parent | |
299 | * @param y row relative to parent | |
300 | * @param width width of widget | |
301 | * @param height height of widget | |
302 | * @param closeAction action to perform when the shell sxits | |
303 | */ | |
304 | public TTerminalWidget(final TWidget parent, final int x, final int y, | |
305 | final int width, final int height, final TAction closeAction) { | |
306 | ||
307 | super(parent, x, y, width, height); | |
308 | ||
309 | this.closeAction = closeAction; | |
4d2c61b4 KL |
310 | |
311 | if (System.getProperty("jexer.TTerminal.shell") != null) { | |
312 | String shell = System.getProperty("jexer.TTerminal.shell"); | |
313 | if (shell.trim().startsWith("ptypipe")) { | |
314 | ptypipe = true; | |
315 | } | |
316 | spawnShell(shell.split("\\s+")); | |
317 | return; | |
318 | } | |
319 | ||
320 | String cmdShellWindows = "cmd.exe"; | |
321 | ||
322 | // You cannot run a login shell in a bare Process interactively, due | |
323 | // to libc's behavior of buffering when stdin/stdout aren't a tty. | |
324 | // Use 'script' instead to run a shell in a pty. And because BSD and | |
325 | // GNU differ on the '-f' vs '-F' flags, we need two different | |
326 | // commands. Lovely. | |
327 | String cmdShellGNU = "script -fqe /dev/null"; | |
328 | String cmdShellBSD = "script -q -F /dev/null"; | |
329 | ||
330 | // ptypipe is another solution that permits dynamic window resizing. | |
331 | String cmdShellPtypipe = "ptypipe /bin/bash --login"; | |
332 | ||
333 | // Spawn a shell and pass its I/O to the other constructor. | |
334 | if ((System.getProperty("jexer.TTerminal.ptypipe") != null) | |
335 | && (System.getProperty("jexer.TTerminal.ptypipe"). | |
336 | equals("true")) | |
337 | ) { | |
338 | ptypipe = true; | |
339 | spawnShell(cmdShellPtypipe.split("\\s+")); | |
340 | } else if (System.getProperty("os.name").startsWith("Windows")) { | |
341 | spawnShell(cmdShellWindows.split("\\s+")); | |
342 | } else if (System.getProperty("os.name").startsWith("Mac")) { | |
343 | spawnShell(cmdShellBSD.split("\\s+")); | |
344 | } else if (System.getProperty("os.name").startsWith("Linux")) { | |
345 | spawnShell(cmdShellGNU.split("\\s+")); | |
346 | } else { | |
347 | // When all else fails, assume GNU. | |
348 | spawnShell(cmdShellGNU.split("\\s+")); | |
349 | } | |
350 | } | |
351 | ||
352 | // ------------------------------------------------------------------------ | |
353 | // TScrollableWidget ------------------------------------------------------ | |
354 | // ------------------------------------------------------------------------ | |
355 | ||
356 | /** | |
357 | * Draw the display buffer. | |
358 | */ | |
359 | @Override | |
360 | public void draw() { | |
361 | int width = getDisplayWidth(); | |
2bc32111 | 362 | |
4d2c61b4 KL |
363 | boolean syncEmulator = false; |
364 | if ((System.currentTimeMillis() - lastUpdateTime >= 25) | |
365 | && (dirty == true) | |
366 | ) { | |
367 | // Too much time has passed, draw it all. | |
368 | syncEmulator = true; | |
369 | } else if (emulator.isReading() && (dirty == false)) { | |
370 | // Wait until the emulator has brought more data in. | |
371 | syncEmulator = false; | |
372 | } else if (!emulator.isReading() && (dirty == true)) { | |
373 | // The emulator won't receive more data, update the display. | |
374 | syncEmulator = true; | |
375 | } | |
376 | ||
377 | if ((syncEmulator == true) | |
378 | || (scrollback == null) | |
379 | || (display == null) | |
380 | ) { | |
381 | // We want to minimize the amount of time we have the emulator | |
382 | // locked. Grab a copy of its display. | |
383 | synchronized (emulator) { | |
384 | // Update the scroll bars | |
385 | reflowData(); | |
386 | ||
387 | if ((scrollback == null) || emulator.isReading()) { | |
388 | scrollback = copyBuffer(emulator.getScrollbackBuffer()); | |
389 | display = copyBuffer(emulator.getDisplayBuffer()); | |
390 | } | |
391 | width = emulator.getWidth(); | |
392 | } | |
393 | dirty = false; | |
394 | } | |
395 | ||
4d2c61b4 KL |
396 | // Put together the visible rows |
397 | int visibleHeight = getHeight(); | |
398 | int visibleBottom = scrollback.size() + display.size() | |
399 | + getVerticalValue(); | |
400 | assert (visibleBottom >= 0); | |
401 | ||
402 | List<DisplayLine> preceedingBlankLines = new ArrayList<DisplayLine>(); | |
403 | int visibleTop = visibleBottom - visibleHeight; | |
404 | if (visibleTop < 0) { | |
405 | for (int i = visibleTop; i < 0; i++) { | |
406 | preceedingBlankLines.add(emulator.getBlankDisplayLine()); | |
407 | } | |
408 | visibleTop = 0; | |
409 | } | |
410 | assert (visibleTop >= 0); | |
411 | ||
412 | List<DisplayLine> displayLines = new ArrayList<DisplayLine>(); | |
413 | displayLines.addAll(scrollback); | |
414 | displayLines.addAll(display); | |
415 | ||
416 | List<DisplayLine> visibleLines = new ArrayList<DisplayLine>(); | |
417 | visibleLines.addAll(preceedingBlankLines); | |
418 | visibleLines.addAll(displayLines.subList(visibleTop, | |
419 | visibleBottom)); | |
420 | ||
421 | visibleHeight -= visibleLines.size(); | |
422 | assert (visibleHeight >= 0); | |
423 | ||
424 | // Now draw the emulator screen | |
425 | int row = 0; | |
426 | for (DisplayLine line: visibleLines) { | |
427 | int widthMax = width; | |
428 | if (line.isDoubleWidth()) { | |
429 | widthMax /= 2; | |
430 | } | |
431 | if (widthMax > getWidth()) { | |
432 | widthMax = getWidth(); | |
433 | } | |
434 | for (int i = 0; i < widthMax; i++) { | |
435 | Cell ch = line.charAt(i); | |
436 | ||
437 | if (ch.isImage()) { | |
438 | putCharXY(i, row, ch); | |
439 | continue; | |
440 | } | |
441 | ||
442 | Cell newCell = new Cell(ch); | |
443 | boolean reverse = line.isReverseColor() ^ ch.isReverse(); | |
444 | newCell.setReverse(false); | |
445 | if (reverse) { | |
446 | if (ch.getForeColorRGB() < 0) { | |
447 | newCell.setBackColor(ch.getForeColor()); | |
448 | newCell.setBackColorRGB(-1); | |
449 | } else { | |
450 | newCell.setBackColorRGB(ch.getForeColorRGB()); | |
451 | } | |
452 | if (ch.getBackColorRGB() < 0) { | |
453 | newCell.setForeColor(ch.getBackColor()); | |
454 | newCell.setForeColorRGB(-1); | |
455 | } else { | |
456 | newCell.setForeColorRGB(ch.getBackColorRGB()); | |
457 | } | |
458 | } | |
459 | if (line.isDoubleWidth()) { | |
460 | putDoubleWidthCharXY(line, (i * 2), row, newCell); | |
461 | } else { | |
462 | putCharXY(i, row, newCell); | |
463 | } | |
464 | } | |
465 | row++; | |
466 | if (row == getHeight()) { | |
467 | // Don't overwrite the box edge | |
468 | break; | |
469 | } | |
470 | } | |
471 | CellAttributes background = new CellAttributes(); | |
472 | // Fill in the blank lines on bottom | |
473 | for (int i = 0; i < visibleHeight; i++) { | |
474 | hLineXY(0, i + row, getWidth(), ' ', background); | |
475 | } | |
476 | ||
477 | } | |
478 | ||
479 | /** | |
2bc32111 | 480 | * Handle widget close. |
4d2c61b4 KL |
481 | */ |
482 | @Override | |
483 | public void close() { | |
484 | emulator.close(); | |
485 | if (shell != null) { | |
486 | terminateShellChildProcess(); | |
487 | shell.destroy(); | |
488 | shell = null; | |
489 | } | |
490 | } | |
491 | ||
492 | /** | |
493 | * Handle window/screen resize events. | |
494 | * | |
495 | * @param resize resize event | |
496 | */ | |
497 | @Override | |
498 | public void onResize(final TResizeEvent resize) { | |
2bc32111 KL |
499 | // Let TWidget set my size. |
500 | super.onResize(resize); | |
4d2c61b4 KL |
501 | |
502 | // Synchronize against the emulator so we don't stomp on its reader | |
503 | // thread. | |
504 | synchronized (emulator) { | |
505 | ||
506 | if (resize.getType() == TResizeEvent.Type.WIDGET) { | |
507 | // Resize the scroll bars | |
508 | reflowData(); | |
509 | placeScrollbars(); | |
510 | ||
511 | // Get out of scrollback | |
512 | setVerticalValue(0); | |
513 | ||
514 | if (ptypipe) { | |
515 | emulator.setWidth(getWidth()); | |
516 | emulator.setHeight(getHeight()); | |
517 | ||
518 | emulator.writeRemote("\033[8;" + getHeight() + ";" + | |
519 | getWidth() + "t"); | |
520 | } | |
c7a75ad3 KL |
521 | |
522 | // Pass the correct text cell width/height to the emulator | |
523 | if (getScreen() != null) { | |
524 | emulator.setTextWidth(getScreen().getTextWidth()); | |
525 | emulator.setTextHeight(getScreen().getTextHeight()); | |
526 | } | |
4d2c61b4 KL |
527 | } |
528 | return; | |
529 | ||
530 | } // synchronized (emulator) | |
531 | } | |
532 | ||
533 | /** | |
534 | * Resize scrollbars for a new width/height. | |
535 | */ | |
536 | @Override | |
537 | public void reflowData() { | |
538 | ||
539 | // Synchronize against the emulator so we don't stomp on its reader | |
540 | // thread. | |
541 | synchronized (emulator) { | |
542 | ||
543 | // Pull cursor information | |
544 | readEmulatorState(); | |
545 | ||
546 | // Vertical scrollbar | |
547 | setTopValue(getHeight() | |
548 | - (emulator.getScrollbackBuffer().size() | |
549 | + emulator.getDisplayBuffer().size())); | |
550 | setVerticalBigChange(getHeight()); | |
551 | ||
552 | } // synchronized (emulator) | |
553 | } | |
554 | ||
555 | /** | |
556 | * Handle keystrokes. | |
557 | * | |
558 | * @param keypress keystroke event | |
559 | */ | |
560 | @Override | |
561 | public void onKeypress(final TKeypressEvent keypress) { | |
562 | if (hideMouseWhenTyping) { | |
563 | typingHidMouse = true; | |
564 | } | |
565 | ||
566 | // Scrollback up/down | |
567 | if (keypress.equals(kbShiftPgUp) | |
568 | || keypress.equals(kbCtrlPgUp) | |
569 | || keypress.equals(kbAltPgUp) | |
570 | ) { | |
571 | bigVerticalDecrement(); | |
572 | return; | |
573 | } | |
574 | if (keypress.equals(kbShiftPgDn) | |
575 | || keypress.equals(kbCtrlPgDn) | |
576 | || keypress.equals(kbAltPgDn) | |
577 | ) { | |
578 | bigVerticalIncrement(); | |
579 | return; | |
580 | } | |
581 | ||
582 | if (emulator.isReading()) { | |
583 | // Get out of scrollback | |
584 | setVerticalValue(0); | |
585 | emulator.addUserEvent(keypress); | |
586 | ||
587 | // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if | |
588 | // this is kBEnter then also send kbCtrlJ. | |
589 | if (keypress.equals(kbEnter)) { | |
590 | if (System.getProperty("os.name").startsWith("Windows") | |
591 | && (System.getProperty("jexer.TTerminal.cmdHack", | |
592 | "true").equals("true")) | |
593 | ) { | |
594 | emulator.addUserEvent(new TKeypressEvent(kbCtrlJ)); | |
595 | } | |
596 | } | |
597 | ||
598 | readEmulatorState(); | |
599 | return; | |
600 | } | |
601 | ||
602 | // Process is closed, honor "normal" TUI keystrokes | |
603 | super.onKeypress(keypress); | |
604 | } | |
605 | ||
606 | /** | |
607 | * Handle mouse press events. | |
608 | * | |
609 | * @param mouse mouse button press event | |
610 | */ | |
611 | @Override | |
612 | public void onMouseDown(final TMouseEvent mouse) { | |
613 | if (hideMouseWhenTyping) { | |
614 | typingHidMouse = false; | |
615 | } | |
616 | ||
617 | // If the emulator is tracking mouse buttons, it needs to see wheel | |
618 | // events. | |
619 | if (emulator.getMouseProtocol() == ECMA48.MouseProtocol.OFF) { | |
620 | if (mouse.isMouseWheelUp()) { | |
621 | verticalDecrement(); | |
622 | return; | |
623 | } | |
624 | if (mouse.isMouseWheelDown()) { | |
625 | verticalIncrement(); | |
626 | return; | |
627 | } | |
628 | } | |
629 | if (mouseOnEmulator(mouse)) { | |
630 | emulator.addUserEvent(mouse); | |
631 | readEmulatorState(); | |
632 | return; | |
633 | } | |
634 | ||
635 | // Emulator didn't consume it, pass it on | |
636 | super.onMouseDown(mouse); | |
637 | } | |
638 | ||
639 | /** | |
640 | * Handle mouse release events. | |
641 | * | |
642 | * @param mouse mouse button release event | |
643 | */ | |
644 | @Override | |
645 | public void onMouseUp(final TMouseEvent mouse) { | |
646 | if (hideMouseWhenTyping) { | |
647 | typingHidMouse = false; | |
648 | } | |
649 | ||
650 | if (mouseOnEmulator(mouse)) { | |
651 | emulator.addUserEvent(mouse); | |
652 | readEmulatorState(); | |
653 | return; | |
654 | } | |
655 | ||
656 | // Emulator didn't consume it, pass it on | |
657 | super.onMouseUp(mouse); | |
658 | } | |
659 | ||
660 | /** | |
661 | * Handle mouse motion events. | |
662 | * | |
663 | * @param mouse mouse motion event | |
664 | */ | |
665 | @Override | |
666 | public void onMouseMotion(final TMouseEvent mouse) { | |
667 | if (hideMouseWhenTyping) { | |
668 | typingHidMouse = false; | |
669 | } | |
670 | ||
671 | if (mouseOnEmulator(mouse)) { | |
672 | emulator.addUserEvent(mouse); | |
673 | readEmulatorState(); | |
674 | return; | |
675 | } | |
676 | ||
677 | // Emulator didn't consume it, pass it on | |
678 | super.onMouseMotion(mouse); | |
679 | } | |
680 | ||
681 | // ------------------------------------------------------------------------ | |
682 | // TTerminalWidget -------------------------------------------------------- | |
683 | // ------------------------------------------------------------------------ | |
684 | ||
685 | /** | |
686 | * Get the desired window title. | |
687 | * | |
688 | * @return the title | |
689 | */ | |
690 | public String getTitle() { | |
691 | return title; | |
692 | } | |
693 | ||
694 | /** | |
2bc32111 | 695 | * Returns true if this widget does not want the application-wide mouse |
4d2c61b4 KL |
696 | * cursor drawn over it. |
697 | * | |
2bc32111 | 698 | * @return true if this widget does not want the application-wide mouse |
4d2c61b4 KL |
699 | * cursor drawn over it |
700 | */ | |
701 | public boolean hasHiddenMouse() { | |
702 | return (emulator.hasHiddenMousePointer() || typingHidMouse); | |
703 | } | |
704 | ||
705 | /** | |
706 | * See if the terminal is still running. | |
707 | * | |
708 | * @return if true, we are still connected to / reading from the remote | |
709 | * side | |
710 | */ | |
711 | public boolean isReading() { | |
712 | return emulator.isReading(); | |
713 | } | |
714 | ||
715 | /** | |
716 | * Convert a string array to a whitespace-separated string. | |
717 | * | |
718 | * @param array the string array | |
719 | * @return a single string | |
720 | */ | |
721 | private String stringArrayToString(final String [] array) { | |
722 | StringBuilder sb = new StringBuilder(array[0].length()); | |
723 | for (int i = 0; i < array.length; i++) { | |
724 | sb.append(array[i]); | |
725 | if (i < array.length - 1) { | |
726 | sb.append(' '); | |
727 | } | |
728 | } | |
729 | return sb.toString(); | |
730 | } | |
731 | ||
732 | /** | |
733 | * Spawn the shell. | |
734 | * | |
735 | * @param command the command line to execute | |
736 | */ | |
737 | private void spawnShell(final String [] command) { | |
738 | ||
739 | /* | |
740 | System.err.printf("spawnShell(): '%s'\n", | |
741 | stringArrayToString(command)); | |
742 | */ | |
743 | ||
744 | // We will have vScroller for its data fields and mouse event | |
745 | // handling, but do not want to draw it. | |
746 | vScroller = new TVScroller(null, getWidth(), 0, getHeight()); | |
747 | vScroller.setVisible(false); | |
748 | setBottomValue(0); | |
749 | ||
750 | title = i18n.getString("windowTitle"); | |
751 | ||
752 | // Assume XTERM | |
753 | ECMA48.DeviceType deviceType = ECMA48.DeviceType.XTERM; | |
754 | ||
755 | try { | |
756 | ProcessBuilder pb = new ProcessBuilder(command); | |
757 | Map<String, String> env = pb.environment(); | |
758 | env.put("TERM", ECMA48.deviceTypeTerm(deviceType)); | |
759 | env.put("LANG", ECMA48.deviceTypeLang(deviceType, "en")); | |
760 | env.put("COLUMNS", "80"); | |
761 | env.put("LINES", "24"); | |
762 | pb.redirectErrorStream(true); | |
763 | shell = pb.start(); | |
764 | emulator = new ECMA48(deviceType, shell.getInputStream(), | |
765 | shell.getOutputStream(), this); | |
766 | } catch (IOException e) { | |
767 | messageBox(i18n.getString("errorLaunchingShellTitle"), | |
768 | MessageFormat.format(i18n.getString("errorLaunchingShellText"), | |
769 | e.getMessage())); | |
770 | } | |
771 | ||
772 | // Setup the scroll bars | |
773 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(), | |
774 | getHeight())); | |
775 | ||
4d2c61b4 KL |
776 | // Hide mouse when typing option |
777 | if (System.getProperty("jexer.TTerminal.hideMouseWhenTyping", | |
778 | "true").equals("false")) { | |
779 | ||
780 | hideMouseWhenTyping = false; | |
781 | } | |
782 | } | |
783 | ||
784 | /** | |
785 | * Terminate the child of the 'script' process used on POSIX. This may | |
786 | * or may not work. | |
787 | */ | |
788 | private void terminateShellChildProcess() { | |
789 | int pid = -1; | |
790 | if (shell.getClass().getName().equals("java.lang.UNIXProcess")) { | |
791 | /* get the PID on unix/linux systems */ | |
792 | try { | |
793 | Field field = shell.getClass().getDeclaredField("pid"); | |
794 | field.setAccessible(true); | |
795 | pid = field.getInt(shell); | |
796 | } catch (Throwable e) { | |
797 | // SQUASH, this didn't work. Just bail out quietly. | |
798 | return; | |
799 | } | |
800 | } | |
801 | if (pid != -1) { | |
802 | // shell.destroy() works successfully at killing this side of | |
803 | // 'script'. But we need to make sure the other side (child | |
804 | // process) is also killed. | |
805 | String [] cmdKillIt = { | |
806 | "pkill", "-P", Integer.toString(pid) | |
807 | }; | |
808 | try { | |
809 | Runtime.getRuntime().exec(cmdKillIt); | |
810 | } catch (Throwable e) { | |
811 | // SQUASH, this didn't work. Just bail out quietly. | |
812 | return; | |
813 | } | |
814 | } | |
815 | } | |
816 | ||
817 | /** | |
818 | * Hook for subclasses to be notified of the shell termination. | |
819 | */ | |
820 | public void onShellExit() { | |
2bc32111 KL |
821 | TApplication app = getApplication(); |
822 | if (app != null) { | |
823 | app.invokeLater(new Runnable() { | |
824 | public void run() { | |
825 | if (closeAction != null) { | |
a524aa2e | 826 | closeAction.DO(TTerminalWidget.this); |
2bc32111 KL |
827 | } |
828 | if (getApplication() != null) { | |
829 | getApplication().postEvent(new TMenuEvent( | |
830 | TMenu.MID_REPAINT)); | |
831 | } | |
832 | } | |
833 | }); | |
4d2c61b4 | 834 | } |
4d2c61b4 KL |
835 | } |
836 | ||
837 | /** | |
838 | * Copy out variables from the emulator that TTerminal has to expose on | |
839 | * screen. | |
840 | */ | |
841 | private void readEmulatorState() { | |
842 | // Synchronize against the emulator so we don't stomp on its reader | |
843 | // thread. | |
844 | synchronized (emulator) { | |
845 | ||
846 | setCursorX(emulator.getCursorX()); | |
847 | setCursorY(emulator.getCursorY() | |
848 | + (getHeight() - emulator.getHeight()) | |
849 | - getVerticalValue()); | |
850 | setCursorVisible(emulator.isCursorVisible()); | |
851 | if (getCursorX() > getWidth()) { | |
852 | setCursorVisible(false); | |
853 | } | |
854 | if ((getCursorY() >= getHeight()) || (getCursorY() < 0)) { | |
855 | setCursorVisible(false); | |
856 | } | |
857 | if (emulator.getScreenTitle().length() > 0) { | |
858 | // Only update the title if the shell is still alive | |
859 | if (shell != null) { | |
860 | title = emulator.getScreenTitle(); | |
861 | } | |
862 | } | |
863 | ||
864 | // Check to see if the shell has died. | |
865 | if (!emulator.isReading() && (shell != null)) { | |
866 | try { | |
867 | int rc = shell.exitValue(); | |
868 | // The emulator exited on its own, all is fine | |
869 | title = MessageFormat.format(i18n. | |
870 | getString("windowTitleCompleted"), title, rc); | |
871 | exitValue = rc; | |
872 | shell = null; | |
873 | emulator.close(); | |
874 | onShellExit(); | |
875 | } catch (IllegalThreadStateException e) { | |
876 | // The emulator thread has exited, but the shell Process | |
877 | // hasn't figured that out yet. Do nothing, we will see | |
878 | // this in a future tick. | |
879 | } | |
880 | } else if (emulator.isReading() && (shell != null)) { | |
881 | // The shell might be dead, let's check | |
882 | try { | |
883 | int rc = shell.exitValue(); | |
884 | // If we got here, the shell died. | |
885 | title = MessageFormat.format(i18n. | |
886 | getString("windowTitleCompleted"), title, rc); | |
887 | exitValue = rc; | |
888 | shell = null; | |
889 | emulator.close(); | |
890 | onShellExit(); | |
891 | } catch (IllegalThreadStateException e) { | |
892 | // The shell is still running, do nothing. | |
893 | } | |
894 | } | |
895 | ||
896 | } // synchronized (emulator) | |
897 | } | |
898 | ||
899 | /** | |
900 | * Check if a mouse press/release/motion event coordinate is over the | |
901 | * emulator. | |
902 | * | |
903 | * @param mouse a mouse-based event | |
904 | * @return whether or not the mouse is on the emulator | |
905 | */ | |
906 | private boolean mouseOnEmulator(final TMouseEvent mouse) { | |
907 | ||
908 | if (!emulator.isReading()) { | |
909 | return false; | |
910 | } | |
911 | ||
912 | if ((mouse.getX() >= 0) | |
913 | && (mouse.getX() < getWidth() - 1) | |
914 | && (mouse.getY() >= 0) | |
915 | && (mouse.getY() < getHeight()) | |
916 | ) { | |
917 | return true; | |
918 | } | |
919 | return false; | |
920 | } | |
921 | ||
922 | /** | |
923 | * Copy a display buffer. | |
924 | * | |
925 | * @param buffer the buffer to copy | |
926 | * @return a deep copy of the buffer's data | |
927 | */ | |
928 | private List<DisplayLine> copyBuffer(final List<DisplayLine> buffer) { | |
929 | ArrayList<DisplayLine> result = new ArrayList<DisplayLine>(buffer.size()); | |
930 | for (DisplayLine line: buffer) { | |
931 | result.add(new DisplayLine(line)); | |
932 | } | |
933 | return result; | |
934 | } | |
935 | ||
936 | /** | |
937 | * Draw glyphs for a double-width or double-height VT100 cell to two | |
938 | * screen cells. | |
939 | * | |
940 | * @param line the line this VT100 cell is in | |
941 | * @param x the X position to draw the left half to | |
942 | * @param y the Y position to draw to | |
943 | * @param cell the cell to draw | |
944 | */ | |
945 | private void putDoubleWidthCharXY(final DisplayLine line, final int x, | |
946 | final int y, final Cell cell) { | |
947 | ||
948 | int textWidth = getScreen().getTextWidth(); | |
949 | int textHeight = getScreen().getTextHeight(); | |
950 | boolean cursorBlinkVisible = true; | |
951 | ||
952 | if (getScreen() instanceof SwingTerminal) { | |
953 | SwingTerminal terminal = (SwingTerminal) getScreen(); | |
954 | cursorBlinkVisible = terminal.getCursorBlinkVisible(); | |
955 | } else if (getScreen() instanceof ECMA48Terminal) { | |
956 | ECMA48Terminal terminal = (ECMA48Terminal) getScreen(); | |
957 | ||
958 | if (!terminal.hasSixel()) { | |
959 | // The backend does not have sixel support, draw this as text | |
960 | // and bail out. | |
961 | putCharXY(x, y, cell); | |
962 | putCharXY(x + 1, y, ' ', cell); | |
963 | return; | |
964 | } | |
965 | cursorBlinkVisible = blinkState; | |
966 | } else { | |
967 | // We don't know how to dray glyphs to this screen, draw them as | |
968 | // text and bail out. | |
969 | putCharXY(x, y, cell); | |
970 | putCharXY(x + 1, y, ' ', cell); | |
971 | return; | |
972 | } | |
973 | ||
974 | if ((textWidth != lastTextWidth) || (textHeight != lastTextHeight)) { | |
975 | // Screen size has changed, reset the font. | |
976 | setupFont(textHeight); | |
977 | lastTextWidth = textWidth; | |
978 | lastTextHeight = textHeight; | |
979 | } | |
980 | assert (doubleFont != null); | |
981 | ||
982 | BufferedImage image; | |
983 | if (line.getDoubleHeight() == 1) { | |
984 | // Double-height top half: don't draw the underline. | |
985 | Cell newCell = new Cell(cell); | |
986 | newCell.setUnderline(false); | |
987 | image = doubleFont.getImage(newCell, textWidth * 2, textHeight * 2, | |
988 | cursorBlinkVisible); | |
989 | } else { | |
990 | image = doubleFont.getImage(cell, textWidth * 2, textHeight * 2, | |
991 | cursorBlinkVisible); | |
992 | } | |
993 | ||
994 | // Now that we have the double-wide glyph drawn, copy the right | |
995 | // pieces of it to the cells. | |
996 | Cell left = new Cell(cell); | |
997 | Cell right = new Cell(cell); | |
998 | right.setChar(' '); | |
999 | BufferedImage leftImage = null; | |
1000 | BufferedImage rightImage = null; | |
1001 | /* | |
1002 | System.err.println("image " + image + " textWidth " + textWidth + | |
1003 | " textHeight " + textHeight); | |
1004 | */ | |
1005 | ||
1006 | switch (line.getDoubleHeight()) { | |
1007 | case 1: | |
1008 | // Top half double height | |
1009 | leftImage = image.getSubimage(0, 0, textWidth, textHeight); | |
1010 | rightImage = image.getSubimage(textWidth, 0, textWidth, textHeight); | |
1011 | break; | |
1012 | case 2: | |
1013 | // Bottom half double height | |
1014 | leftImage = image.getSubimage(0, textHeight, textWidth, textHeight); | |
1015 | rightImage = image.getSubimage(textWidth, textHeight, | |
1016 | textWidth, textHeight); | |
1017 | break; | |
1018 | default: | |
1019 | // Either single height double-width, or error fallback | |
1020 | BufferedImage wideImage = new BufferedImage(textWidth * 2, | |
1021 | textHeight, BufferedImage.TYPE_INT_ARGB); | |
1022 | Graphics2D grWide = wideImage.createGraphics(); | |
1023 | grWide.drawImage(image, 0, 0, wideImage.getWidth(), | |
1024 | wideImage.getHeight(), null); | |
1025 | grWide.dispose(); | |
1026 | leftImage = wideImage.getSubimage(0, 0, textWidth, textHeight); | |
1027 | rightImage = wideImage.getSubimage(textWidth, 0, textWidth, | |
1028 | textHeight); | |
1029 | break; | |
1030 | } | |
1031 | left.setImage(leftImage); | |
1032 | right.setImage(rightImage); | |
1033 | // Since we have image data, ditch the character here. Otherwise, a | |
1034 | // drawBoxShadow() over the terminal window will show the characters | |
1035 | // which looks wrong. | |
1036 | left.setChar(' '); | |
1037 | right.setChar(' '); | |
1038 | putCharXY(x, y, left); | |
1039 | putCharXY(x + 1, y, right); | |
1040 | } | |
1041 | ||
1042 | /** | |
1043 | * Set up the double-width font. | |
1044 | * | |
1045 | * @param fontSize the size of font to request for the single-width font. | |
1046 | * The double-width font will be 2x this value. | |
1047 | */ | |
1048 | private void setupFont(final int fontSize) { | |
1049 | doubleFont = GlyphMaker.getInstance(fontSize * 2); | |
1050 | ||
1051 | // Special case: the ECMA48 backend needs to have a timer to drive | |
1052 | // its blink state. | |
1053 | if (getScreen() instanceof jexer.backend.ECMA48Terminal) { | |
1054 | if (!haveTimer) { | |
1055 | // Blink every 500 millis. | |
1056 | long millis = 500; | |
1057 | getApplication().addTimer(millis, true, | |
1058 | new TAction() { | |
1059 | public void DO() { | |
1060 | blinkState = !blinkState; | |
1061 | getApplication().doRepaint(); | |
1062 | } | |
1063 | } | |
1064 | ); | |
1065 | haveTimer = true; | |
1066 | } | |
1067 | } | |
1068 | } | |
1069 | ||
1070 | // ------------------------------------------------------------------------ | |
1071 | // DisplayListener -------------------------------------------------------- | |
1072 | // ------------------------------------------------------------------------ | |
1073 | ||
1074 | /** | |
1075 | * Called by emulator when fresh data has come in. | |
1076 | */ | |
1077 | public void displayChanged() { | |
1078 | dirty = true; | |
1079 | getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT)); | |
1080 | } | |
1081 | ||
1082 | /** | |
1083 | * Function to call to obtain the display width. | |
1084 | * | |
1085 | * @return the number of columns in the display | |
1086 | */ | |
1087 | public int getDisplayWidth() { | |
1088 | if (ptypipe) { | |
1089 | return getWidth(); | |
1090 | } | |
1091 | return 80; | |
1092 | } | |
1093 | ||
1094 | /** | |
1095 | * Function to call to obtain the display height. | |
1096 | * | |
1097 | * @return the number of rows in the display | |
1098 | */ | |
1099 | public int getDisplayHeight() { | |
1100 | if (ptypipe) { | |
1101 | return getHeight(); | |
1102 | } | |
1103 | return 24; | |
1104 | } | |
1105 | ||
1106 | } |