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
.Graphics2D
;
32 import java
.awt
.image
.BufferedImage
;
34 import java
.io
.IOException
;
35 import java
.lang
.reflect
.Field
;
36 import java
.text
.MessageFormat
;
37 import java
.util
.List
;
39 import java
.util
.ResourceBundle
;
41 import jexer
.backend
.ECMA48Terminal
;
42 import jexer
.backend
.GlyphMaker
;
43 import jexer
.backend
.SwingTerminal
;
44 import jexer
.bits
.Cell
;
45 import jexer
.event
.TCommandEvent
;
46 import jexer
.event
.TKeypressEvent
;
47 import jexer
.event
.TMenuEvent
;
48 import jexer
.event
.TMouseEvent
;
49 import jexer
.event
.TResizeEvent
;
50 import jexer
.menu
.TMenu
;
51 import jexer
.tterminal
.DisplayLine
;
52 import jexer
.tterminal
.DisplayListener
;
53 import jexer
.tterminal
.ECMA48
;
54 import static jexer
.TCommand
.*;
55 import static jexer
.TKeypress
.*;
58 * TTerminalWidget exposes a ECMA-48 / ANSI X3.64 style terminal in a widget.
60 public class TTerminalWidget
extends TScrollableWidget
61 implements DisplayListener
, EditMenuUser
{
66 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TTerminalWidget
.class.getName());
68 // ------------------------------------------------------------------------
69 // Variables --------------------------------------------------------------
70 // ------------------------------------------------------------------------
75 private ECMA48 emulator
;
78 * The Process created by the shell spawning constructor.
80 private Process shell
;
83 * If true, something called 'ptypipe' is on the PATH and executable.
85 private static boolean ptypipeOnPath
= false;
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 .
92 private boolean ptypipe
= false;
97 private GlyphMaker doubleFont
;
100 * Last text width value.
102 private int lastTextWidth
= -1;
105 * Last text height value.
107 private int lastTextHeight
= -1;
110 * The blink state, used only by ECMA48 backend and when double-width
111 * chars must be drawn.
113 private boolean blinkState
= true;
116 * Timer flag, used only by ECMA48 backend and when double-width chars
119 private boolean haveTimer
= false;
122 * The last seen visible display.
124 private List
<DisplayLine
> display
;
127 * If true, the display has changed and needs updating.
129 private volatile boolean dirty
= true;
132 * Time that the display was last updated.
134 private long lastUpdateTime
= 0;
137 * If true, hide the mouse after typing a keystroke.
139 private boolean hideMouseWhenTyping
= true;
142 * If true, the mouse should not be displayed because a keystroke was
145 private boolean typingHidMouse
= false;
148 * The return value from the emulator.
150 private int exitValue
= -1;
153 * Title to expose to a window.
155 private String title
= "";
158 * Action to perform when the terminal exits.
160 private TAction closeAction
= null;
162 // ------------------------------------------------------------------------
163 // Constructors -----------------------------------------------------------
164 // ------------------------------------------------------------------------
167 * Static constructor.
174 * Public constructor spawns a custom command line.
176 * @param parent parent widget
177 * @param x column relative to parent
178 * @param y row relative to parent
179 * @param commandLine the command line to execute
181 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
182 final String commandLine
) {
184 this(parent
, x
, y
, commandLine
.split("\\s+"));
188 * Public constructor spawns a custom command line.
190 * @param parent parent widget
191 * @param x column relative to parent
192 * @param y row relative to parent
193 * @param command the command line to execute
195 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
196 final String
[] command
) {
198 this(parent
, x
, y
, command
, null);
202 * Public constructor spawns a custom command line.
204 * @param parent parent widget
205 * @param x column relative to parent
206 * @param y row relative to parent
207 * @param command the command line to execute
208 * @param closeAction action to perform when the shell exits
210 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
211 final String
[] command
, final TAction closeAction
) {
213 this(parent
, x
, y
, 80, 24, command
, closeAction
);
217 * Public constructor spawns a custom command line.
219 * @param parent parent widget
220 * @param x column relative to parent
221 * @param y row relative to parent
222 * @param width width of widget
223 * @param height height of widget
224 * @param command the command line to execute
225 * @param closeAction action to perform when the shell exits
227 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
228 final int width
, final int height
, final String
[] command
,
229 final TAction closeAction
) {
231 super(parent
, x
, y
, width
, height
);
233 this.closeAction
= closeAction
;
235 String
[] fullCommand
;
237 // Spawn a shell and pass its I/O to the other constructor.
238 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
239 && (System
.getProperty("jexer.TTerminal.ptypipe").
243 fullCommand
= new String
[command
.length
+ 1];
244 fullCommand
[0] = "ptypipe";
245 System
.arraycopy(command
, 0, fullCommand
, 1, command
.length
);
246 } else if (System
.getProperty("jexer.TTerminal.ptypipe",
247 "auto").equals("auto")
248 && (ptypipeOnPath
== true)
251 fullCommand
= new String
[command
.length
+ 1];
252 fullCommand
[0] = "ptypipe";
253 System
.arraycopy(command
, 0, fullCommand
, 1, command
.length
);
254 } else if (System
.getProperty("os.name").startsWith("Windows")) {
255 fullCommand
= new String
[3];
256 fullCommand
[0] = "cmd";
257 fullCommand
[1] = "/c";
258 fullCommand
[2] = stringArrayToString(command
);
259 } else if (System
.getProperty("os.name").startsWith("Mac")) {
260 fullCommand
= new String
[6];
261 fullCommand
[0] = "script";
262 fullCommand
[1] = "-q";
263 fullCommand
[2] = "-F";
264 fullCommand
[3] = "/dev/null";
265 fullCommand
[4] = "-c";
266 fullCommand
[5] = stringArrayToString(command
);
268 // Default: behave like Linux
269 if (System
.getProperty("jexer.TTerminal.setsid",
270 "true").equals("false")
272 fullCommand
= new String
[5];
273 fullCommand
[0] = "script";
274 fullCommand
[1] = "-fqe";
275 fullCommand
[2] = "/dev/null";
276 fullCommand
[3] = "-c";
277 fullCommand
[4] = stringArrayToString(command
);
279 fullCommand
= new String
[6];
280 fullCommand
[0] = "setsid";
281 fullCommand
[1] = "script";
282 fullCommand
[2] = "-fqe";
283 fullCommand
[3] = "/dev/null";
284 fullCommand
[4] = "-c";
285 fullCommand
[5] = stringArrayToString(command
);
288 spawnShell(fullCommand
);
292 * Public constructor spawns a shell.
294 * @param parent parent widget
295 * @param x column relative to parent
296 * @param y row relative to parent
298 public TTerminalWidget(final TWidget parent
, final int x
, final int y
) {
299 this(parent
, x
, y
, (TAction
) null);
303 * Public constructor spawns a shell.
305 * @param parent parent widget
306 * @param x column relative to parent
307 * @param y row relative to parent
308 * @param closeAction action to perform when the shell exits
310 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
311 final TAction closeAction
) {
313 this(parent
, x
, y
, 80, 24, closeAction
);
317 * Public constructor spawns a shell.
319 * @param parent parent widget
320 * @param x column relative to parent
321 * @param y row relative to parent
322 * @param width width of widget
323 * @param height height of widget
324 * @param closeAction action to perform when the shell exits
326 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
327 final int width
, final int height
, final TAction closeAction
) {
329 super(parent
, x
, y
, width
, height
);
331 this.closeAction
= closeAction
;
333 if (System
.getProperty("jexer.TTerminal.shell") != null) {
334 String shell
= System
.getProperty("jexer.TTerminal.shell");
335 if (shell
.trim().startsWith("ptypipe")) {
338 spawnShell(shell
.split("\\s+"));
342 String cmdShellWindows
= "cmd.exe";
344 // You cannot run a login shell in a bare Process interactively, due
345 // to libc's behavior of buffering when stdin/stdout aren't a tty.
346 // Use 'script' instead to run a shell in a pty. And because BSD and
347 // GNU differ on the '-f' vs '-F' flags, we need two different
349 String cmdShellGNU
= "script -fqe /dev/null";
350 String cmdShellGNUSetsid
= "setsid script -fqe /dev/null";
351 String cmdShellBSD
= "script -q -F /dev/null";
353 // ptypipe is another solution that permits dynamic window resizing.
354 String cmdShellPtypipe
= "ptypipe /bin/bash --login";
356 // Spawn a shell and pass its I/O to the other constructor.
357 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
358 && (System
.getProperty("jexer.TTerminal.ptypipe").
362 spawnShell(cmdShellPtypipe
.split("\\s+"));
363 } else if (System
.getProperty("jexer.TTerminal.ptypipe",
364 "auto").equals("auto")
365 && (ptypipeOnPath
== true)
368 spawnShell(cmdShellPtypipe
.split("\\s+"));
369 } else if (System
.getProperty("os.name").startsWith("Windows")) {
370 spawnShell(cmdShellWindows
.split("\\s+"));
371 } else if (System
.getProperty("os.name").startsWith("Mac")) {
372 spawnShell(cmdShellBSD
.split("\\s+"));
373 } else if (System
.getProperty("os.name").startsWith("Linux")) {
374 if (System
.getProperty("jexer.TTerminal.setsid",
375 "true").equals("false")
377 spawnShell(cmdShellGNU
.split("\\s+"));
379 spawnShell(cmdShellGNUSetsid
.split("\\s+"));
382 // When all else fails, assume GNU.
383 spawnShell(cmdShellGNU
.split("\\s+"));
387 // ------------------------------------------------------------------------
388 // Event handlers ---------------------------------------------------------
389 // ------------------------------------------------------------------------
392 * Handle window/screen resize events.
394 * @param resize resize event
397 public void onResize(final TResizeEvent resize
) {
398 // Let TWidget set my size.
399 super.onResize(resize
);
401 if (emulator
== null) {
405 // Synchronize against the emulator so we don't stomp on its reader
407 synchronized (emulator
) {
409 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
410 // Resize the scroll bars
414 // Get out of scrollback
418 emulator
.setWidth(getWidth());
419 emulator
.setHeight(getHeight());
421 emulator
.writeRemote("\033[8;" + getHeight() + ";" +
425 // Pass the correct text cell width/height to the emulator
426 if (getScreen() != null) {
427 emulator
.setTextWidth(getScreen().getTextWidth());
428 emulator
.setTextHeight(getScreen().getTextHeight());
433 } // synchronized (emulator)
439 * @param keypress keystroke event
442 public void onKeypress(final TKeypressEvent keypress
) {
443 if (hideMouseWhenTyping
) {
444 typingHidMouse
= true;
447 // Scrollback up/down
448 if (keypress
.equals(kbShiftPgUp
)
449 || keypress
.equals(kbCtrlPgUp
)
450 || keypress
.equals(kbAltPgUp
)
452 bigVerticalDecrement();
456 if (keypress
.equals(kbShiftPgDn
)
457 || keypress
.equals(kbCtrlPgDn
)
458 || keypress
.equals(kbAltPgDn
)
460 bigVerticalIncrement();
465 if ((emulator
!= null) && (emulator
.isReading())) {
466 // Get out of scrollback
468 emulator
.addUserEvent(keypress
);
470 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
471 // this is kBEnter then also send kbCtrlJ.
472 if (keypress
.equals(kbEnter
)) {
473 if (System
.getProperty("os.name").startsWith("Windows")
474 && (System
.getProperty("jexer.TTerminal.cmdHack",
475 "true").equals("true"))
477 emulator
.addUserEvent(new TKeypressEvent(kbCtrlJ
));
485 // Process is closed, honor "normal" TUI keystrokes
486 super.onKeypress(keypress
);
490 * Handle mouse press events.
492 * @param mouse mouse button press event
495 public void onMouseDown(final TMouseEvent mouse
) {
496 if (hideMouseWhenTyping
) {
497 typingHidMouse
= false;
500 if (emulator
!= null) {
501 // If the emulator is tracking mouse buttons, it needs to see
503 if (emulator
.getMouseProtocol() == ECMA48
.MouseProtocol
.OFF
) {
504 if (mouse
.isMouseWheelUp()) {
509 if (mouse
.isMouseWheelDown()) {
515 if (mouseOnEmulator(mouse
)) {
516 emulator
.addUserEvent(mouse
);
522 // Emulator didn't consume it, pass it on
523 super.onMouseDown(mouse
);
527 * Handle mouse release events.
529 * @param mouse mouse button release event
532 public void onMouseUp(final TMouseEvent mouse
) {
533 if (hideMouseWhenTyping
) {
534 typingHidMouse
= false;
537 if ((emulator
!= null) && (mouseOnEmulator(mouse
))) {
538 emulator
.addUserEvent(mouse
);
543 // Emulator didn't consume it, pass it on
544 super.onMouseUp(mouse
);
548 * Handle mouse motion events.
550 * @param mouse mouse motion event
553 public void onMouseMotion(final TMouseEvent mouse
) {
554 if (hideMouseWhenTyping
) {
555 typingHidMouse
= false;
558 if ((emulator
!= null) && (mouseOnEmulator(mouse
))) {
559 emulator
.addUserEvent(mouse
);
564 // Emulator didn't consume it, pass it on
565 super.onMouseMotion(mouse
);
569 * Handle posted command events.
571 * @param command command event
574 public void onCommand(final TCommandEvent command
) {
575 if (emulator
== null) {
579 if (command
.equals(cmPaste
)) {
580 // Paste text from clipboard.
581 String text
= getClipboard().pasteText();
583 for (int i
= 0; i
< text
.length(); ) {
584 int ch
= text
.codePointAt(i
);
585 emulator
.addUserEvent(new TKeypressEvent(false, 0, ch
,
586 false, false, false));
587 i
+= Character
.charCount(ch
);
594 // ------------------------------------------------------------------------
595 // TScrollableWidget ------------------------------------------------------
596 // ------------------------------------------------------------------------
599 * Draw the display buffer.
603 if (emulator
== null) {
607 int width
= getDisplayWidth();
609 boolean syncEmulator
= false;
610 if (System
.currentTimeMillis() - lastUpdateTime
>= 50) {
611 // Too much time has passed, draw it all.
613 } else if (emulator
.isReading() && (dirty
== false)) {
614 // Wait until the emulator has brought more data in.
615 syncEmulator
= false;
616 } else if (!emulator
.isReading() && (dirty
== true)) {
617 // The emulator won't receive more data, update the display.
621 if ((syncEmulator
== true)
624 // We want to minimize the amount of time we have the emulator
625 // locked. Grab a copy of its display.
626 synchronized (emulator
) {
627 // Update the scroll bars
631 // We lost the connection, onShellExit() called an action
632 // that ultimately removed this widget from the UI
633 // hierarchy, so no one cares if we update the display.
638 if ((display
== null) || emulator
.isReading()) {
639 display
= emulator
.getVisibleDisplay(getHeight(),
640 -getVerticalValue());
641 assert (display
.size() == getHeight());
643 width
= emulator
.getWidth();
648 // Now draw the emulator screen
650 for (DisplayLine line
: display
) {
651 int widthMax
= width
;
652 if (line
.isDoubleWidth()) {
655 if (widthMax
> getWidth()) {
656 widthMax
= getWidth();
658 for (int i
= 0; i
< widthMax
; i
++) {
659 Cell ch
= line
.charAt(i
);
662 putCharXY(i
, row
, ch
);
666 Cell newCell
= new Cell(ch
);
667 boolean reverse
= line
.isReverseColor() ^ ch
.isReverse();
668 newCell
.setReverse(false);
670 if (ch
.getForeColorRGB() < 0) {
671 newCell
.setBackColor(ch
.getForeColor());
672 newCell
.setBackColorRGB(-1);
674 newCell
.setBackColorRGB(ch
.getForeColorRGB());
676 if (ch
.getBackColorRGB() < 0) {
677 newCell
.setForeColor(ch
.getBackColor());
678 newCell
.setForeColorRGB(-1);
680 newCell
.setForeColorRGB(ch
.getBackColorRGB());
683 if (line
.isDoubleWidth()) {
684 putDoubleWidthCharXY(line
, (i
* 2), row
, newCell
);
686 putCharXY(i
, row
, newCell
);
694 * Set current value of the vertical scroll.
696 * @param value the new scroll value
699 public void setVerticalValue(final int value
) {
700 super.setVerticalValue(value
);
705 * Perform a small step change up.
708 public void verticalDecrement() {
709 super.verticalDecrement();
714 * Perform a small step change down.
717 public void verticalIncrement() {
718 super.verticalIncrement();
723 * Perform a big step change up.
725 public void bigVerticalDecrement() {
726 super.bigVerticalDecrement();
731 * Perform a big step change down.
733 public void bigVerticalIncrement() {
734 super.bigVerticalIncrement();
739 * Go to the top edge of the vertical scroller.
741 public void toTop() {
747 * Go to the bottom edge of the vertical scroller.
749 public void toBottom() {
755 * Handle widget close.
758 public void close() {
759 if (emulator
!= null) {
763 terminateShellChildProcess();
770 * Resize scrollbars for a new width/height.
773 public void reflowData() {
774 if (emulator
== null) {
778 // Synchronize against the emulator so we don't stomp on its reader
780 synchronized (emulator
) {
782 // Pull cursor information
785 // Vertical scrollbar
786 setTopValue(getHeight()
787 - (emulator
.getScrollbackBuffer().size()
788 + emulator
.getDisplayBuffer().size()));
789 setVerticalBigChange(getHeight());
791 } // synchronized (emulator)
794 // ------------------------------------------------------------------------
795 // TTerminalWidget --------------------------------------------------------
796 // ------------------------------------------------------------------------
799 * Check for 'ptypipe' on the path. If available, set ptypipeOnPath.
801 private static void checkForPtypipe() {
802 String systemPath
= System
.getenv("PATH");
803 if (systemPath
== null) {
807 String
[] paths
= systemPath
.split(File
.pathSeparator
);
811 if (paths
.length
== 0) {
814 for (int i
= 0; i
< paths
.length
; i
++) {
815 File path
= new File(paths
[i
]);
816 if (path
.exists() && path
.isDirectory()) {
817 File
[] files
= path
.listFiles();
821 if (files
.length
== 0) {
824 for (int j
= 0; j
< files
.length
; j
++) {
825 File file
= files
[j
];
826 if (file
.canExecute() && file
.getName().equals("ptypipe")) {
827 ptypipeOnPath
= true;
836 * Get the desired window title.
840 public String
getTitle() {
845 * Returns true if this widget does not want the application-wide mouse
846 * cursor drawn over it.
848 * @return true if this widget does not want the application-wide mouse
849 * cursor drawn over it
851 public boolean hasHiddenMouse() {
852 if (emulator
== null) {
855 return (emulator
.hasHiddenMousePointer() || typingHidMouse
);
859 * See if the terminal is still running.
861 * @return if true, we are still connected to / reading from the remote
864 public boolean isReading() {
865 if (emulator
== null) {
868 return emulator
.isReading();
872 * Convert a string array to a whitespace-separated string.
874 * @param array the string array
875 * @return a single string
877 private String
stringArrayToString(final String
[] array
) {
878 StringBuilder sb
= new StringBuilder(array
[0].length());
879 for (int i
= 0; i
< array
.length
; i
++) {
881 if (i
< array
.length
- 1) {
885 return sb
.toString();
891 * @param command the command line to execute
893 private void spawnShell(final String
[] command
) {
896 System.err.printf("spawnShell(): '%s'\n",
897 stringArrayToString(command));
900 // We will have vScroller for its data fields and mouse event
901 // handling, but do not want to draw it.
902 vScroller
= new TVScroller(null, getWidth(), 0, getHeight());
903 vScroller
.setVisible(false);
906 title
= i18n
.getString("windowTitle");
909 ECMA48
.DeviceType deviceType
= ECMA48
.DeviceType
.XTERM
;
912 ProcessBuilder pb
= new ProcessBuilder(command
);
913 Map
<String
, String
> env
= pb
.environment();
914 env
.put("TERM", ECMA48
.deviceTypeTerm(deviceType
));
915 env
.put("LANG", ECMA48
.deviceTypeLang(deviceType
, "en"));
916 env
.put("COLUMNS", "80");
917 env
.put("LINES", "24");
918 pb
.redirectErrorStream(true);
920 emulator
= new ECMA48(deviceType
, shell
.getInputStream(),
921 shell
.getOutputStream(), this);
922 } catch (IOException e
) {
923 messageBox(i18n
.getString("errorLaunchingShellTitle"),
924 MessageFormat
.format(i18n
.getString("errorLaunchingShellText"),
928 // Setup the scroll bars
929 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
932 // Hide mouse when typing option
933 if (System
.getProperty("jexer.TTerminal.hideMouseWhenTyping",
934 "true").equals("false")) {
936 hideMouseWhenTyping
= false;
941 * Terminate the child of the 'script' process used on POSIX. This may
944 private void terminateShellChildProcess() {
946 if (shell
.getClass().getName().equals("java.lang.UNIXProcess")) {
947 /* get the PID on unix/linux systems */
949 Field field
= shell
.getClass().getDeclaredField("pid");
950 field
.setAccessible(true);
951 pid
= field
.getInt(shell
);
952 } catch (Throwable e
) {
953 // SQUASH, this didn't work. Just bail out quietly.
958 // shell.destroy() works successfully at killing this side of
959 // 'script'. But we need to make sure the other side (child
960 // process) is also killed.
961 String
[] cmdKillIt
= {
962 "pkill", "-P", Integer
.toString(pid
)
965 Runtime
.getRuntime().exec(cmdKillIt
);
966 } catch (Throwable e
) {
967 // SQUASH, this didn't work. Just bail out quietly.
974 * Hook for subclasses to be notified of the shell termination.
976 public void onShellExit() {
977 TApplication app
= getApplication();
979 if (closeAction
!= null) {
980 // We have to put this action inside invokeLater() because it
981 // could be executed during draw() when syncing with ECMA48.
982 app
.invokeLater(new Runnable() {
984 closeAction
.DO(TTerminalWidget
.this);
993 * Copy out variables from the emulator that TTerminal has to expose on
996 private void readEmulatorState() {
997 if (emulator
== null) {
1001 // Synchronize against the emulator so we don't stomp on its reader
1003 synchronized (emulator
) {
1005 setCursorX(emulator
.getCursorX());
1006 setCursorY(emulator
.getCursorY()
1007 + (getHeight() - emulator
.getHeight())
1008 - getVerticalValue());
1009 setCursorVisible(emulator
.isCursorVisible());
1010 if (getCursorX() > getWidth()) {
1011 setCursorVisible(false);
1013 if ((getCursorY() >= getHeight()) || (getCursorY() < 0)) {
1014 setCursorVisible(false);
1016 if (emulator
.getScreenTitle().length() > 0) {
1017 // Only update the title if the shell is still alive
1018 if (shell
!= null) {
1019 title
= emulator
.getScreenTitle();
1023 // Check to see if the shell has died.
1024 if (!emulator
.isReading() && (shell
!= null)) {
1026 int rc
= shell
.exitValue();
1027 // The emulator exited on its own, all is fine
1028 title
= MessageFormat
.format(i18n
.
1029 getString("windowTitleCompleted"), title
, rc
);
1034 } catch (IllegalThreadStateException e
) {
1035 // The emulator thread has exited, but the shell Process
1036 // hasn't figured that out yet. Do nothing, we will see
1037 // this in a future tick.
1039 } else if (emulator
.isReading() && (shell
!= null)) {
1040 // The shell might be dead, let's check
1042 int rc
= shell
.exitValue();
1043 // If we got here, the shell died.
1044 title
= MessageFormat
.format(i18n
.
1045 getString("windowTitleCompleted"), title
, rc
);
1050 } catch (IllegalThreadStateException e
) {
1051 // The shell is still running, do nothing.
1055 } // synchronized (emulator)
1059 * Wait for a period of time to get output from the launched process.
1061 * @param millis millis to wait for, or 0 to wait forever
1062 * @return true if the launched process has emitted something
1064 public boolean waitForOutput(final int millis
) {
1065 if (emulator
== null) {
1068 return emulator
.waitForOutput(millis
);
1072 * Check if a mouse press/release/motion event coordinate is over the
1075 * @param mouse a mouse-based event
1076 * @return whether or not the mouse is on the emulator
1078 private boolean mouseOnEmulator(final TMouseEvent mouse
) {
1079 if (emulator
== null) {
1083 if (!emulator
.isReading()) {
1087 if ((mouse
.getX() >= 0)
1088 && (mouse
.getX() < getWidth() - 1)
1089 && (mouse
.getY() >= 0)
1090 && (mouse
.getY() < getHeight())
1098 * Draw glyphs for a double-width or double-height VT100 cell to two
1101 * @param line the line this VT100 cell is in
1102 * @param x the X position to draw the left half to
1103 * @param y the Y position to draw to
1104 * @param cell the cell to draw
1106 private void putDoubleWidthCharXY(final DisplayLine line
, final int x
,
1107 final int y
, final Cell cell
) {
1109 int textWidth
= getScreen().getTextWidth();
1110 int textHeight
= getScreen().getTextHeight();
1111 boolean cursorBlinkVisible
= true;
1113 if (getScreen() instanceof SwingTerminal
) {
1114 SwingTerminal terminal
= (SwingTerminal
) getScreen();
1115 cursorBlinkVisible
= terminal
.getCursorBlinkVisible();
1116 } else if (getScreen() instanceof ECMA48Terminal
) {
1117 ECMA48Terminal terminal
= (ECMA48Terminal
) getScreen();
1119 if (!terminal
.hasSixel()) {
1120 // The backend does not have sixel support, draw this as text
1122 putCharXY(x
, y
, cell
);
1123 putCharXY(x
+ 1, y
, ' ', cell
);
1126 cursorBlinkVisible
= blinkState
;
1128 // We don't know how to dray glyphs to this screen, draw them as
1129 // text and bail out.
1130 putCharXY(x
, y
, cell
);
1131 putCharXY(x
+ 1, y
, ' ', cell
);
1135 if ((textWidth
!= lastTextWidth
) || (textHeight
!= lastTextHeight
)) {
1136 // Screen size has changed, reset the font.
1137 setupFont(textHeight
);
1138 lastTextWidth
= textWidth
;
1139 lastTextHeight
= textHeight
;
1141 assert (doubleFont
!= null);
1143 BufferedImage image
;
1144 if (line
.getDoubleHeight() == 1) {
1145 // Double-height top half: don't draw the underline.
1146 Cell newCell
= new Cell(cell
);
1147 newCell
.setUnderline(false);
1148 image
= doubleFont
.getImage(newCell
, textWidth
* 2, textHeight
* 2,
1149 cursorBlinkVisible
);
1151 image
= doubleFont
.getImage(cell
, textWidth
* 2, textHeight
* 2,
1152 cursorBlinkVisible
);
1155 // Now that we have the double-wide glyph drawn, copy the right
1156 // pieces of it to the cells.
1157 Cell left
= new Cell(cell
);
1158 Cell right
= new Cell(cell
);
1160 BufferedImage leftImage
= null;
1161 BufferedImage rightImage
= null;
1163 System.err.println("image " + image + " textWidth " + textWidth +
1164 " textHeight " + textHeight);
1167 switch (line
.getDoubleHeight()) {
1169 // Top half double height
1170 leftImage
= image
.getSubimage(0, 0, textWidth
, textHeight
);
1171 rightImage
= image
.getSubimage(textWidth
, 0, textWidth
, textHeight
);
1174 // Bottom half double height
1175 leftImage
= image
.getSubimage(0, textHeight
, textWidth
, textHeight
);
1176 rightImage
= image
.getSubimage(textWidth
, textHeight
,
1177 textWidth
, textHeight
);
1180 // Either single height double-width, or error fallback
1181 BufferedImage wideImage
= new BufferedImage(textWidth
* 2,
1182 textHeight
, BufferedImage
.TYPE_INT_ARGB
);
1183 Graphics2D grWide
= wideImage
.createGraphics();
1184 grWide
.drawImage(image
, 0, 0, wideImage
.getWidth(),
1185 wideImage
.getHeight(), null);
1187 leftImage
= wideImage
.getSubimage(0, 0, textWidth
, textHeight
);
1188 rightImage
= wideImage
.getSubimage(textWidth
, 0, textWidth
,
1192 left
.setImage(leftImage
);
1193 right
.setImage(rightImage
);
1194 // Since we have image data, ditch the character here. Otherwise, a
1195 // drawBoxShadow() over the terminal window will show the characters
1196 // which looks wrong.
1199 putCharXY(x
, y
, left
);
1200 putCharXY(x
+ 1, y
, right
);
1204 * Set up the double-width font.
1206 * @param fontSize the size of font to request for the single-width font.
1207 * The double-width font will be 2x this value.
1209 private void setupFont(final int fontSize
) {
1210 doubleFont
= GlyphMaker
.getInstance(fontSize
* 2);
1212 // Special case: the ECMA48 backend needs to have a timer to drive
1214 if (getScreen() instanceof jexer
.backend
.ECMA48Terminal
) {
1216 // Blink every 500 millis.
1218 getApplication().addTimer(millis
, true,
1221 blinkState
= !blinkState
;
1222 getApplication().doRepaint();
1231 // ------------------------------------------------------------------------
1232 // DisplayListener --------------------------------------------------------
1233 // ------------------------------------------------------------------------
1236 * Called by emulator when fresh data has come in.
1238 public void displayChanged() {
1239 if (emulator
!= null) {
1240 // Force sync here: EMCA48.run() thread might be setting
1241 // dirty=true while TTerminalWdiget.draw() is setting
1242 // dirty=false. If these writes start interleaving, the display
1243 // stops getting updated.
1244 synchronized (emulator
) {
1250 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
1254 * Function to call to obtain the display width.
1256 * @return the number of columns in the display
1258 public int getDisplayWidth() {
1266 * Function to call to obtain the display height.
1268 * @return the number of rows in the display
1270 public int getDisplayHeight() {
1278 * Get the exit value for the emulator.
1280 * @return exit value
1282 public int getExitValue() {
1286 // ------------------------------------------------------------------------
1287 // EditMenuUser -----------------------------------------------------------
1288 // ------------------------------------------------------------------------
1291 * Check if the cut menu item should be enabled.
1293 * @return true if the cut menu item should be enabled
1295 public boolean isEditMenuCut() {
1300 * Check if the copy menu item should be enabled.
1302 * @return true if the copy menu item should be enabled
1304 public boolean isEditMenuCopy() {
1309 * Check if the paste menu item should be enabled.
1311 * @return true if the paste menu item should be enabled
1313 public boolean isEditMenuPaste() {
1318 * Check if the clear menu item should be enabled.
1320 * @return true if the clear menu item should be enabled
1322 public boolean isEditMenuClear() {