2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.awt
.image
.BufferedImage
;
33 import java
.awt
.FontMetrics
;
34 import java
.awt
.Graphics2D
;
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
;
44 import java
.util
.ResourceBundle
;
46 import jexer
.backend
.ECMA48Terminal
;
47 import jexer
.backend
.MultiScreen
;
48 import jexer
.backend
.SwingTerminal
;
49 import jexer
.bits
.Cell
;
50 import jexer
.bits
.CellAttributes
;
51 import jexer
.event
.TKeypressEvent
;
52 import jexer
.event
.TMenuEvent
;
53 import jexer
.event
.TMouseEvent
;
54 import jexer
.event
.TResizeEvent
;
55 import jexer
.menu
.TMenu
;
56 import jexer
.tterminal
.DisplayLine
;
57 import jexer
.tterminal
.DisplayListener
;
58 import jexer
.tterminal
.ECMA48
;
59 import static jexer
.TKeypress
.*;
62 * TTerminalWindow exposes a ECMA-48 / ANSI X3.64 style terminal in a window.
64 public class TTerminalWindow
extends TScrollableWindow
65 implements DisplayListener
{
70 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TTerminalWindow
.class.getName());
72 // ------------------------------------------------------------------------
73 // Variables --------------------------------------------------------------
74 // ------------------------------------------------------------------------
79 private ECMA48 emulator
;
82 * The Process created by the shell spawning constructor.
84 private Process shell
;
87 * If true, we are using the ptypipe utility to support dynamic window
88 * resizing. ptypipe is available at
89 * https://gitlab.com/klamonte/ptypipe .
91 private boolean ptypipe
= false;
94 * If true, close the window when the shell exits.
96 private boolean closeOnExit
= false;
99 * System-dependent Y adjustment for text in the character cell
102 private int doubleTextAdjustY
= 0;
105 * System-dependent X adjustment for text in the character cell
108 private int doubleTextAdjustX
= 0;
111 * Descent of a character cell in pixels (double-height).
113 private int doubleMaxDescent
= 0;
118 private Font doubleFont
= null;
121 * Last text width value.
123 private int lastTextWidth
= -1;
126 * Last text height value.
128 private int lastTextHeight
= -1;
131 * A cache of previously-rendered double-width glyphs.
133 private Map
<Cell
, BufferedImage
> glyphCache
;
135 // ------------------------------------------------------------------------
136 // Constructors -----------------------------------------------------------
137 // ------------------------------------------------------------------------
140 * Public constructor spawns a custom command line.
142 * @param application TApplication that manages this window
143 * @param x column relative to parent
144 * @param y row relative to parent
145 * @param commandLine the command line to execute
147 public TTerminalWindow(final TApplication application
, final int x
,
148 final int y
, final String commandLine
) {
150 this(application
, x
, y
, RESIZABLE
, commandLine
.split("\\s+"),
151 System
.getProperty("jexer.TTerminal.closeOnExit",
152 "false").equals("true"));
156 * Public constructor spawns a custom command line.
158 * @param application TApplication that manages this window
159 * @param x column relative to parent
160 * @param y row relative to parent
161 * @param commandLine the command line to execute
162 * @param closeOnExit if true, close the window when the command exits
164 public TTerminalWindow(final TApplication application
, final int x
,
165 final int y
, final String commandLine
, final boolean closeOnExit
) {
167 this(application
, x
, y
, RESIZABLE
, commandLine
.split("\\s+"),
172 * Public constructor spawns a custom command line.
174 * @param application TApplication that manages this window
175 * @param x column relative to parent
176 * @param y row relative to parent
177 * @param flags mask of CENTERED, MODAL, or RESIZABLE
178 * @param command the command line to execute
180 public TTerminalWindow(final TApplication application
, final int x
,
181 final int y
, final int flags
, final String
[] command
) {
183 this(application
, x
, y
, flags
, command
,
184 System
.getProperty("jexer.TTerminal.closeOnExit",
185 "false").equals("true"));
189 * Public constructor spawns a custom command line.
191 * @param application TApplication that manages this window
192 * @param x column relative to parent
193 * @param y row relative to parent
194 * @param flags mask of CENTERED, MODAL, or RESIZABLE
195 * @param command the command line to execute
196 * @param closeOnExit if true, close the window when the command exits
198 public TTerminalWindow(final TApplication application
, final int x
,
199 final int y
, final int flags
, final String
[] command
,
200 final boolean closeOnExit
) {
202 super(application
, i18n
.getString("windowTitle"), x
, y
,
203 80 + 2, 24 + 2, flags
);
205 this.closeOnExit
= closeOnExit
;
207 String
[] fullCommand
;
209 // Spawn a shell and pass its I/O to the other constructor.
210 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
211 && (System
.getProperty("jexer.TTerminal.ptypipe").
215 fullCommand
= new String
[command
.length
+ 1];
216 fullCommand
[0] = "ptypipe";
217 System
.arraycopy(command
, 0, fullCommand
, 1, command
.length
);
218 } else if (System
.getProperty("os.name").startsWith("Windows")) {
219 fullCommand
= new String
[3];
220 fullCommand
[0] = "cmd";
221 fullCommand
[1] = "/c";
222 fullCommand
[2] = stringArrayToString(command
);
223 } else if (System
.getProperty("os.name").startsWith("Mac")) {
224 fullCommand
= new String
[6];
225 fullCommand
[0] = "script";
226 fullCommand
[1] = "-q";
227 fullCommand
[2] = "-F";
228 fullCommand
[3] = "/dev/null";
229 fullCommand
[4] = "-c";
230 fullCommand
[5] = stringArrayToString(command
);
232 // Default: behave like Linux
233 fullCommand
= new String
[5];
234 fullCommand
[0] = "script";
235 fullCommand
[1] = "-fqe";
236 fullCommand
[2] = "/dev/null";
237 fullCommand
[3] = "-c";
238 fullCommand
[4] = stringArrayToString(command
);
240 spawnShell(fullCommand
);
244 * Public constructor spawns a shell.
246 * @param application TApplication that manages this window
247 * @param x column relative to parent
248 * @param y row relative to parent
249 * @param flags mask of CENTERED, MODAL, or RESIZABLE
251 public TTerminalWindow(final TApplication application
, final int x
,
252 final int y
, final int flags
) {
254 this(application
, x
, y
, flags
,
255 System
.getProperty("jexer.TTerminal.closeOnExit",
256 "false").equals("true"));
261 * Public constructor spawns a shell.
263 * @param application TApplication that manages this window
264 * @param x column relative to parent
265 * @param y row relative to parent
266 * @param flags mask of CENTERED, MODAL, or RESIZABLE
267 * @param closeOnExit if true, close the window when the shell exits
269 public TTerminalWindow(final TApplication application
, final int x
,
270 final int y
, final int flags
, final boolean closeOnExit
) {
272 super(application
, i18n
.getString("windowTitle"), x
, y
,
273 80 + 2, 24 + 2, flags
);
275 this.closeOnExit
= closeOnExit
;
277 String cmdShellWindows
= "cmd.exe";
279 // You cannot run a login shell in a bare Process interactively, due
280 // to libc's behavior of buffering when stdin/stdout aren't a tty.
281 // Use 'script' instead to run a shell in a pty. And because BSD and
282 // GNU differ on the '-f' vs '-F' flags, we need two different
284 String cmdShellGNU
= "script -fqe /dev/null";
285 String cmdShellBSD
= "script -q -F /dev/null";
287 // ptypipe is another solution that permits dynamic window resizing.
288 String cmdShellPtypipe
= "ptypipe /bin/bash --login";
290 // Spawn a shell and pass its I/O to the other constructor.
291 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
292 && (System
.getProperty("jexer.TTerminal.ptypipe").
296 spawnShell(cmdShellPtypipe
.split("\\s+"));
297 } else if (System
.getProperty("os.name").startsWith("Windows")) {
298 spawnShell(cmdShellWindows
.split("\\s+"));
299 } else if (System
.getProperty("os.name").startsWith("Mac")) {
300 spawnShell(cmdShellBSD
.split("\\s+"));
301 } else if (System
.getProperty("os.name").startsWith("Linux")) {
302 spawnShell(cmdShellGNU
.split("\\s+"));
304 // When all else fails, assume GNU.
305 spawnShell(cmdShellGNU
.split("\\s+"));
309 // ------------------------------------------------------------------------
310 // TScrollableWindow ------------------------------------------------------
311 // ------------------------------------------------------------------------
314 * Draw the display buffer.
318 // Synchronize against the emulator so we don't stomp on its reader
320 synchronized (emulator
) {
322 // Update the scroll bars
325 // Draw the box using my superclass
328 List
<DisplayLine
> scrollback
= emulator
.getScrollbackBuffer();
329 List
<DisplayLine
> display
= emulator
.getDisplayBuffer();
331 // Put together the visible rows
332 int visibleHeight
= getHeight() - 2;
333 int visibleBottom
= scrollback
.size() + display
.size()
334 + getVerticalValue();
335 assert (visibleBottom
>= 0);
337 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
338 int visibleTop
= visibleBottom
- visibleHeight
;
339 if (visibleTop
< 0) {
340 for (int i
= visibleTop
; i
< 0; i
++) {
341 preceedingBlankLines
.add(emulator
.getBlankDisplayLine());
345 assert (visibleTop
>= 0);
347 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
348 displayLines
.addAll(scrollback
);
349 displayLines
.addAll(display
);
351 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
352 visibleLines
.addAll(preceedingBlankLines
);
353 visibleLines
.addAll(displayLines
.subList(visibleTop
,
356 visibleHeight
-= visibleLines
.size();
357 assert (visibleHeight
>= 0);
359 // Now draw the emulator screen
361 for (DisplayLine line
: visibleLines
) {
362 int widthMax
= emulator
.getWidth();
363 if (line
.isDoubleWidth()) {
366 if (widthMax
> getWidth() - 2) {
367 widthMax
= getWidth() - 2;
369 for (int i
= 0; i
< widthMax
; i
++) {
370 Cell ch
= line
.charAt(i
);
371 Cell newCell
= new Cell();
373 boolean reverse
= line
.isReverseColor() ^ ch
.isReverse();
374 newCell
.setReverse(false);
376 if (ch
.getForeColorRGB() < 0) {
377 newCell
.setBackColor(ch
.getForeColor());
378 newCell
.setBackColorRGB(-1);
380 newCell
.setBackColorRGB(ch
.getForeColorRGB());
382 if (ch
.getBackColorRGB() < 0) {
383 newCell
.setForeColor(ch
.getBackColor());
384 newCell
.setForeColorRGB(-1);
386 newCell
.setForeColorRGB(ch
.getBackColorRGB());
389 if (line
.isDoubleWidth()) {
390 putDoubleWidthCharXY(line
, (i
* 2) + 1, row
, newCell
);
392 putCharXY(i
+ 1, row
, newCell
);
396 if (row
== getHeight() - 1) {
397 // Don't overwrite the box edge
401 CellAttributes background
= new CellAttributes();
402 // Fill in the blank lines on bottom
403 for (int i
= 0; i
< visibleHeight
; i
++) {
404 hLineXY(1, i
+ row
, getWidth() - 2, ' ', background
);
407 } // synchronized (emulator)
412 * Handle window close.
415 public void onClose() {
418 terminateShellChildProcess();
425 * Handle window/screen resize events.
427 * @param resize resize event
430 public void onResize(final TResizeEvent resize
) {
432 // Synchronize against the emulator so we don't stomp on its reader
434 synchronized (emulator
) {
436 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
437 // Resize the scroll bars
441 // Get out of scrollback
445 emulator
.setWidth(getWidth() - 2);
446 emulator
.setHeight(getHeight() - 2);
448 emulator
.writeRemote("\033[8;" + (getHeight() - 2) + ";" +
449 (getWidth() - 2) + "t");
454 } // synchronized (emulator)
458 * Resize scrollbars for a new width/height.
461 public void reflowData() {
463 // Synchronize against the emulator so we don't stomp on its reader
465 synchronized (emulator
) {
467 // Pull cursor information
470 // Vertical scrollbar
471 setTopValue(getHeight() - 2
472 - (emulator
.getScrollbackBuffer().size()
473 + emulator
.getDisplayBuffer().size()));
474 setVerticalBigChange(getHeight() - 2);
476 } // synchronized (emulator)
482 * @param keypress keystroke event
485 public void onKeypress(final TKeypressEvent keypress
) {
487 // Scrollback up/down
488 if (keypress
.equals(kbShiftPgUp
)
489 || keypress
.equals(kbCtrlPgUp
)
490 || keypress
.equals(kbAltPgUp
)
492 bigVerticalDecrement();
495 if (keypress
.equals(kbShiftPgDn
)
496 || keypress
.equals(kbCtrlPgDn
)
497 || keypress
.equals(kbAltPgDn
)
499 bigVerticalIncrement();
503 // Synchronize against the emulator so we don't stomp on its reader
505 synchronized (emulator
) {
506 if (emulator
.isReading()) {
507 // Get out of scrollback
509 emulator
.keypress(keypress
.getKey());
511 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
512 // this is kBEnter then also send kbCtrlJ.
513 if (System
.getProperty("os.name").startsWith("Windows")) {
514 if (keypress
.equals(kbEnter
)) {
515 emulator
.keypress(kbCtrlJ
);
524 // Process is closed, honor "normal" TUI keystrokes
525 super.onKeypress(keypress
);
529 * Handle mouse press events.
531 * @param mouse mouse button press event
534 public void onMouseDown(final TMouseEvent mouse
) {
535 if (inWindowMove
|| inWindowResize
) {
536 // TWindow needs to deal with this.
537 super.onMouseDown(mouse
);
541 if (mouse
.isMouseWheelUp()) {
545 if (mouse
.isMouseWheelDown()) {
549 if (mouseOnEmulator(mouse
)) {
550 synchronized (emulator
) {
551 mouse
.setX(mouse
.getX() - 1);
552 mouse
.setY(mouse
.getY() - 1);
553 emulator
.mouse(mouse
);
559 // Emulator didn't consume it, pass it on
560 super.onMouseDown(mouse
);
564 * Handle mouse release events.
566 * @param mouse mouse button release event
569 public void onMouseUp(final TMouseEvent mouse
) {
570 if (inWindowMove
|| inWindowResize
) {
571 // TWindow needs to deal with this.
572 super.onMouseUp(mouse
);
576 if (mouseOnEmulator(mouse
)) {
577 synchronized (emulator
) {
578 mouse
.setX(mouse
.getX() - 1);
579 mouse
.setY(mouse
.getY() - 1);
580 emulator
.mouse(mouse
);
586 // Emulator didn't consume it, pass it on
587 super.onMouseUp(mouse
);
591 * Handle mouse motion events.
593 * @param mouse mouse motion event
596 public void onMouseMotion(final TMouseEvent mouse
) {
597 if (inWindowMove
|| inWindowResize
) {
598 // TWindow needs to deal with this.
599 super.onMouseMotion(mouse
);
603 if (mouseOnEmulator(mouse
)) {
604 synchronized (emulator
) {
605 mouse
.setX(mouse
.getX() - 1);
606 mouse
.setY(mouse
.getY() - 1);
607 emulator
.mouse(mouse
);
613 // Emulator didn't consume it, pass it on
614 super.onMouseMotion(mouse
);
617 // ------------------------------------------------------------------------
618 // TTerminalWindow --------------------------------------------------------
619 // ------------------------------------------------------------------------
622 * Claim the keystrokes the emulator will need.
624 private void addShortcutKeys() {
625 addShortcutKeypress(kbCtrlA
);
626 addShortcutKeypress(kbCtrlB
);
627 addShortcutKeypress(kbCtrlC
);
628 addShortcutKeypress(kbCtrlD
);
629 addShortcutKeypress(kbCtrlE
);
630 addShortcutKeypress(kbCtrlF
);
631 addShortcutKeypress(kbCtrlG
);
632 addShortcutKeypress(kbCtrlH
);
633 addShortcutKeypress(kbCtrlU
);
634 addShortcutKeypress(kbCtrlJ
);
635 addShortcutKeypress(kbCtrlK
);
636 addShortcutKeypress(kbCtrlL
);
637 addShortcutKeypress(kbCtrlM
);
638 addShortcutKeypress(kbCtrlN
);
639 addShortcutKeypress(kbCtrlO
);
640 addShortcutKeypress(kbCtrlP
);
641 addShortcutKeypress(kbCtrlQ
);
642 addShortcutKeypress(kbCtrlR
);
643 addShortcutKeypress(kbCtrlS
);
644 addShortcutKeypress(kbCtrlT
);
645 addShortcutKeypress(kbCtrlU
);
646 addShortcutKeypress(kbCtrlV
);
647 addShortcutKeypress(kbCtrlW
);
648 addShortcutKeypress(kbCtrlX
);
649 addShortcutKeypress(kbCtrlY
);
650 addShortcutKeypress(kbCtrlZ
);
651 addShortcutKeypress(kbF1
);
652 addShortcutKeypress(kbF2
);
653 addShortcutKeypress(kbF3
);
654 addShortcutKeypress(kbF4
);
655 addShortcutKeypress(kbF5
);
656 addShortcutKeypress(kbF6
);
657 addShortcutKeypress(kbF7
);
658 addShortcutKeypress(kbF8
);
659 addShortcutKeypress(kbF9
);
660 addShortcutKeypress(kbF10
);
661 addShortcutKeypress(kbF11
);
662 addShortcutKeypress(kbF12
);
663 addShortcutKeypress(kbAltA
);
664 addShortcutKeypress(kbAltB
);
665 addShortcutKeypress(kbAltC
);
666 addShortcutKeypress(kbAltD
);
667 addShortcutKeypress(kbAltE
);
668 addShortcutKeypress(kbAltF
);
669 addShortcutKeypress(kbAltG
);
670 addShortcutKeypress(kbAltH
);
671 addShortcutKeypress(kbAltU
);
672 addShortcutKeypress(kbAltJ
);
673 addShortcutKeypress(kbAltK
);
674 addShortcutKeypress(kbAltL
);
675 addShortcutKeypress(kbAltM
);
676 addShortcutKeypress(kbAltN
);
677 addShortcutKeypress(kbAltO
);
678 addShortcutKeypress(kbAltP
);
679 addShortcutKeypress(kbAltQ
);
680 addShortcutKeypress(kbAltR
);
681 addShortcutKeypress(kbAltS
);
682 addShortcutKeypress(kbAltT
);
683 addShortcutKeypress(kbAltU
);
684 addShortcutKeypress(kbAltV
);
685 addShortcutKeypress(kbAltW
);
686 addShortcutKeypress(kbAltX
);
687 addShortcutKeypress(kbAltY
);
688 addShortcutKeypress(kbAltZ
);
692 * Convert a string array to a whitespace-separated string.
694 * @param array the string array
695 * @return a single string
697 private String
stringArrayToString(final String
[] array
) {
698 StringBuilder sb
= new StringBuilder(array
[0].length());
699 for (int i
= 0; i
< array
.length
; i
++) {
701 if (i
< array
.length
- 1) {
705 return sb
.toString();
711 * @param command the command line to execute
713 private void spawnShell(final String
[] command
) {
716 System.err.printf("spawnShell(): '%s'\n",
717 stringArrayToString(command));
720 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
724 ECMA48
.DeviceType deviceType
= ECMA48
.DeviceType
.XTERM
;
727 ProcessBuilder pb
= new ProcessBuilder(command
);
728 Map
<String
, String
> env
= pb
.environment();
729 env
.put("TERM", ECMA48
.deviceTypeTerm(deviceType
));
730 env
.put("LANG", ECMA48
.deviceTypeLang(deviceType
, "en"));
731 env
.put("COLUMNS", "80");
732 env
.put("LINES", "24");
733 pb
.redirectErrorStream(true);
735 emulator
= new ECMA48(deviceType
, shell
.getInputStream(),
736 shell
.getOutputStream(), this);
737 } catch (IOException e
) {
738 messageBox(i18n
.getString("errorLaunchingShellTitle"),
739 MessageFormat
.format(i18n
.getString("errorLaunchingShellText"),
743 // Setup the scroll bars
744 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
747 // Claim the keystrokes the emulator will need.
751 newStatusBar(i18n
.getString("statusBarRunning"));
755 * Terminate the child of the 'script' process used on POSIX. This may
758 private void terminateShellChildProcess() {
760 if (shell
.getClass().getName().equals("java.lang.UNIXProcess")) {
761 /* get the PID on unix/linux systems */
763 Field field
= shell
.getClass().getDeclaredField("pid");
764 field
.setAccessible(true);
765 pid
= field
.getInt(shell
);
766 } catch (Throwable e
) {
767 // SQUASH, this didn't work. Just bail out quietly.
772 // shell.destroy() works successfully at killing this side of
773 // 'script'. But we need to make sure the other side (child
774 // process) is also killed.
775 String
[] cmdKillIt
= {
776 "pkill", "-P", Integer
.toString(pid
)
779 Runtime
.getRuntime().exec(cmdKillIt
);
780 } catch (Throwable e
) {
781 // SQUASH, this didn't work. Just bail out quietly.
788 * Called by emulator when fresh data has come in.
790 public void displayChanged() {
791 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
795 * Function to call to obtain the display width.
797 * @return the number of columns in the display
799 public int getDisplayWidth() {
801 return getWidth() - 2;
807 * Function to call to obtain the display height.
809 * @return the number of rows in the display
811 public int getDisplayHeight() {
813 return getHeight() - 2;
819 * Hook for subclasses to be notified of the shell termination.
821 public void onShellExit() {
825 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
829 * Copy out variables from the emulator that TTerminal has to expose on
832 private void readEmulatorState() {
833 // Synchronize against the emulator so we don't stomp on its reader
835 synchronized (emulator
) {
836 setHiddenMouse(emulator
.hasHiddenMousePointer());
838 setCursorX(emulator
.getCursorX() + 1);
839 setCursorY(emulator
.getCursorY() + 1
840 + (getHeight() - 2 - emulator
.getHeight())
841 - getVerticalValue());
842 setCursorVisible(emulator
.isCursorVisible());
843 if (getCursorX() > getWidth() - 2) {
844 setCursorVisible(false);
846 if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
847 setCursorVisible(false);
849 if (emulator
.getScreenTitle().length() > 0) {
850 // Only update the title if the shell is still alive
852 setTitle(emulator
.getScreenTitle());
856 // Check to see if the shell has died.
857 if (!emulator
.isReading() && (shell
!= null)) {
859 int rc
= shell
.exitValue();
860 // The emulator exited on its own, all is fine
861 setTitle(MessageFormat
.format(i18n
.
862 getString("windowTitleCompleted"), getTitle(), rc
));
865 clearShortcutKeypresses();
866 statusBar
.setText(MessageFormat
.format(i18n
.
867 getString("statusBarCompleted"), rc
));
869 } catch (IllegalThreadStateException e
) {
870 // The emulator thread has exited, but the shell Process
871 // hasn't figured that out yet. Do nothing, we will see
872 // this in a future tick.
874 } else if (emulator
.isReading() && (shell
!= null)) {
875 // The shell might be dead, let's check
877 int rc
= shell
.exitValue();
878 // If we got here, the shell died.
879 setTitle(MessageFormat
.format(i18n
.
880 getString("windowTitleCompleted"), getTitle(), rc
));
883 clearShortcutKeypresses();
884 statusBar
.setText(MessageFormat
.format(i18n
.
885 getString("statusBarCompleted"), rc
));
887 } catch (IllegalThreadStateException e
) {
888 // The shell is still running, do nothing.
892 } // synchronized (emulator)
896 * Check if a mouse press/release/motion event coordinate is over the
899 * @param mouse a mouse-based event
900 * @return whether or not the mouse is on the emulator
902 private boolean mouseOnEmulator(final TMouseEvent mouse
) {
904 synchronized (emulator
) {
905 if (!emulator
.isReading()) {
910 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
911 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
912 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
913 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
921 * Draw glyphs for a double-width or double-height VT100 cell to two
924 * @param line the line this VT100 cell is in
925 * @param x the X position to draw the left half to
926 * @param y the Y position to draw to
927 * @param cell the cell to draw
929 private void putDoubleWidthCharXY(final DisplayLine line
, final int x
,
930 final int y
, final Cell cell
) {
935 if (getScreen() instanceof SwingTerminal
) {
936 SwingTerminal terminal
= (SwingTerminal
) getScreen();
938 textWidth
= terminal
.getTextWidth();
939 textHeight
= terminal
.getTextHeight();
940 } else if (getScreen() instanceof ECMA48Terminal
) {
941 ECMA48Terminal terminal
= (ECMA48Terminal
) getScreen();
943 textWidth
= terminal
.getTextWidth();
944 textHeight
= terminal
.getTextHeight();
946 // We don't know how to dray glyphs to this screen, draw them as
947 // text and bail out.
948 putCharXY(x
, y
, cell
);
949 putCharXY(x
+ 1, y
, ' ', cell
);
953 if ((textWidth
!= lastTextWidth
) || (textHeight
!= lastTextHeight
)) {
954 // Screen size has changed, reset all fonts.
955 setupFonts(textHeight
);
956 lastTextWidth
= textWidth
;
957 lastTextHeight
= textHeight
;
959 assert (doubleFont
!= null);
961 BufferedImage image
= null;
963 image
= glyphCache
.get(cell
);
965 // Generate glyph and draw it to an image.
966 image
= new BufferedImage(textWidth
* 2, textHeight
* 2,
967 BufferedImage
.TYPE_INT_ARGB
);
968 Graphics2D gr2
= image
.createGraphics();
969 gr2
.setFont(doubleFont
);
971 // Draw the background rectangle, then the foreground character.
972 if (getScreen() instanceof ECMA48Terminal
) {
973 // BUG: the background color is coming in the same as the
974 // foreground color. For now, don't draw it.
976 gr2
.setColor(SwingTerminal
.attrToBackgroundColor(cell
));
977 gr2
.fillRect(0, 0, image
.getWidth(), image
.getHeight());
979 gr2
.setColor(SwingTerminal
.attrToForegroundColor(cell
));
980 char [] chars
= new char[1];
981 chars
[0] = cell
.getChar();
982 gr2
.drawChars(chars
, 0, 1, doubleTextAdjustX
,
983 (textHeight
* 2) - doubleMaxDescent
+ doubleTextAdjustY
);
985 if (cell
.isUnderline() && (line
.getDoubleHeight() != 1)) {
986 gr2
.fillRect(0, textHeight
- 2, textWidth
, 2);
990 // Now save this generated image, using a new key that will not
991 // be mutated by invertCell().
992 Cell key
= new Cell();
994 glyphCache
.put(key
, image
);
997 // Now that we have the double-wide glyph drawn, copy the right
998 // pieces of it to the cells.
999 Cell left
= new Cell();
1000 Cell right
= new Cell();
1004 BufferedImage leftImage
= null;
1005 BufferedImage rightImage
= null;
1006 switch (line
.getDoubleHeight()) {
1008 // Top half double height
1009 leftImage
= image
.getSubimage(0, 0, textWidth
, textHeight
);
1010 rightImage
= image
.getSubimage(textWidth
, 0, textWidth
, textHeight
);
1013 // Bottom half double height
1014 leftImage
= image
.getSubimage(0, textHeight
, textWidth
, textHeight
);
1015 rightImage
= image
.getSubimage(textWidth
, textHeight
,
1016 textWidth
, textHeight
);
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);
1026 leftImage
= wideImage
.getSubimage(0, 0, textWidth
, textHeight
);
1027 rightImage
= wideImage
.getSubimage(textWidth
, 0, textWidth
,
1031 left
.setImage(leftImage
);
1032 right
.setImage(rightImage
);
1033 putCharXY(x
, y
, left
);
1034 putCharXY(x
+ 1, y
, right
);
1038 * Set up the single and double-width fonts.
1040 * @param fontSize the size of font to request for the single-width font.
1041 * The double-width font will be 2x this value.
1043 private void setupFonts(final int fontSize
) {
1045 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
1046 InputStream in
= loader
.getResourceAsStream(SwingTerminal
.FONTFILE
);
1047 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
1048 Font terminusDouble
= terminusRoot
.deriveFont(Font
.PLAIN
,
1050 doubleFont
= terminusDouble
;
1051 } catch (java
.awt
.FontFormatException e
) {
1052 new TExceptionDialog(getApplication(), e
);
1053 doubleFont
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
* 2);
1054 } catch (java
.io
.IOException e
) {
1055 new TExceptionDialog(getApplication(), e
);
1056 doubleFont
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
* 2);
1059 // Get font descent.
1060 BufferedImage image
= new BufferedImage(fontSize
* 10, fontSize
* 10,
1061 BufferedImage
.TYPE_INT_ARGB
);
1062 Graphics2D gr
= image
.createGraphics();
1063 gr
.setFont(doubleFont
);
1064 FontMetrics fm
= gr
.getFontMetrics();
1065 doubleMaxDescent
= fm
.getMaxDescent();
1069 // (Re)create the glyph cache.
1070 glyphCache
= new HashMap
<Cell
, BufferedImage
>();