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]
32 import java
.awt
.FontMetrics
;
33 import java
.awt
.Graphics2D
;
34 import java
.awt
.image
.BufferedImage
;
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
.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
.*;
63 * TTerminalWindow exposes a ECMA-48 / ANSI X3.64 style terminal in a window.
65 public class TTerminalWindow
extends TScrollableWindow
66 implements DisplayListener
{
71 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TTerminalWindow
.class.getName());
73 // ------------------------------------------------------------------------
74 // Variables --------------------------------------------------------------
75 // ------------------------------------------------------------------------
80 private ECMA48 emulator
;
83 * The Process created by the shell spawning constructor.
85 private Process shell
;
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;
95 * If true, close the window when the shell exits.
97 private boolean closeOnExit
= false;
100 * Double-height font.
102 private GlyphMaker doubleFont
;
105 * Last text width value.
107 private int lastTextWidth
= -1;
110 * Last text height value.
112 private int lastTextHeight
= -1;
115 * The blink state, used only by ECMA48 backend and when double-width
116 * chars must be drawn.
118 private boolean blinkState
= true;
121 * Timer flag, used only by ECMA48 backend and when double-width chars
124 private boolean haveTimer
= false;
127 * The last seen scrollback lines.
129 private List
<DisplayLine
> scrollback
;
132 * The last seen display lines.
134 private List
<DisplayLine
> display
;
137 * If true, the display has changed and needs updating.
139 private volatile boolean dirty
= true;
142 * Time that the display was last updated.
144 private long lastUpdateTime
= 0;
147 * If true, hide the mouse after typing a keystroke.
149 private boolean hideMouseWhenTyping
= true;
152 * If true, the mouse should not be displayed because a keystroke was
155 private boolean typingHidMouse
= false;
157 // ------------------------------------------------------------------------
158 // Constructors -----------------------------------------------------------
159 // ------------------------------------------------------------------------
162 * Public constructor spawns a custom command line.
164 * @param application TApplication that manages this window
165 * @param x column relative to parent
166 * @param y row relative to parent
167 * @param commandLine the command line to execute
169 public TTerminalWindow(final TApplication application
, final int x
,
170 final int y
, final String commandLine
) {
172 this(application
, x
, y
, RESIZABLE
, commandLine
.split("\\s+"),
173 System
.getProperty("jexer.TTerminal.closeOnExit",
174 "false").equals("true"));
178 * Public constructor spawns a custom command line.
180 * @param application TApplication that manages this window
181 * @param x column relative to parent
182 * @param y row relative to parent
183 * @param commandLine the command line to execute
184 * @param closeOnExit if true, close the window when the command exits
186 public TTerminalWindow(final TApplication application
, final int x
,
187 final int y
, final String commandLine
, final boolean closeOnExit
) {
189 this(application
, x
, y
, RESIZABLE
, commandLine
.split("\\s+"),
194 * Public constructor spawns a custom command line.
196 * @param application TApplication that manages this window
197 * @param x column relative to parent
198 * @param y row relative to parent
199 * @param flags mask of CENTERED, MODAL, or RESIZABLE
200 * @param command the command line to execute
202 public TTerminalWindow(final TApplication application
, final int x
,
203 final int y
, final int flags
, final String
[] command
) {
205 this(application
, x
, y
, flags
, command
,
206 System
.getProperty("jexer.TTerminal.closeOnExit",
207 "false").equals("true"));
211 * Public constructor spawns a custom command line.
213 * @param application TApplication that manages this window
214 * @param x column relative to parent
215 * @param y row relative to parent
216 * @param flags mask of CENTERED, MODAL, or RESIZABLE
217 * @param command the command line to execute
218 * @param closeOnExit if true, close the window when the command exits
220 public TTerminalWindow(final TApplication application
, final int x
,
221 final int y
, final int flags
, final String
[] command
,
222 final boolean closeOnExit
) {
224 super(application
, i18n
.getString("windowTitle"), x
, y
,
225 80 + 2, 24 + 2, flags
);
227 this.closeOnExit
= closeOnExit
;
229 String
[] fullCommand
;
231 // Spawn a shell and pass its I/O to the other constructor.
232 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
233 && (System
.getProperty("jexer.TTerminal.ptypipe").
237 fullCommand
= new String
[command
.length
+ 1];
238 fullCommand
[0] = "ptypipe";
239 System
.arraycopy(command
, 0, fullCommand
, 1, command
.length
);
240 } else if (System
.getProperty("os.name").startsWith("Windows")) {
241 fullCommand
= new String
[3];
242 fullCommand
[0] = "cmd";
243 fullCommand
[1] = "/c";
244 fullCommand
[2] = stringArrayToString(command
);
245 } else if (System
.getProperty("os.name").startsWith("Mac")) {
246 fullCommand
= new String
[6];
247 fullCommand
[0] = "script";
248 fullCommand
[1] = "-q";
249 fullCommand
[2] = "-F";
250 fullCommand
[3] = "/dev/null";
251 fullCommand
[4] = "-c";
252 fullCommand
[5] = stringArrayToString(command
);
254 // Default: behave like Linux
255 fullCommand
= new String
[5];
256 fullCommand
[0] = "script";
257 fullCommand
[1] = "-fqe";
258 fullCommand
[2] = "/dev/null";
259 fullCommand
[3] = "-c";
260 fullCommand
[4] = stringArrayToString(command
);
262 spawnShell(fullCommand
);
266 * Public constructor spawns a shell.
268 * @param application TApplication that manages this window
269 * @param x column relative to parent
270 * @param y row relative to parent
271 * @param flags mask of CENTERED, MODAL, or RESIZABLE
273 public TTerminalWindow(final TApplication application
, final int x
,
274 final int y
, final int flags
) {
276 this(application
, x
, y
, flags
,
277 System
.getProperty("jexer.TTerminal.closeOnExit",
278 "false").equals("true"));
283 * Public constructor spawns a shell.
285 * @param application TApplication that manages this window
286 * @param x column relative to parent
287 * @param y row relative to parent
288 * @param flags mask of CENTERED, MODAL, or RESIZABLE
289 * @param closeOnExit if true, close the window when the shell exits
291 public TTerminalWindow(final TApplication application
, final int x
,
292 final int y
, final int flags
, final boolean closeOnExit
) {
294 super(application
, i18n
.getString("windowTitle"), x
, y
,
295 80 + 2, 24 + 2, flags
);
297 this.closeOnExit
= closeOnExit
;
299 if (System
.getProperty("jexer.TTerminal.shell") != null) {
300 String shell
= System
.getProperty("jexer.TTerminal.shell");
301 if (shell
.trim().startsWith("ptypipe")) {
304 spawnShell(shell
.split("\\s+"));
308 String cmdShellWindows
= "cmd.exe";
310 // You cannot run a login shell in a bare Process interactively, due
311 // to libc's behavior of buffering when stdin/stdout aren't a tty.
312 // Use 'script' instead to run a shell in a pty. And because BSD and
313 // GNU differ on the '-f' vs '-F' flags, we need two different
315 String cmdShellGNU
= "script -fqe /dev/null";
316 String cmdShellBSD
= "script -q -F /dev/null";
318 // ptypipe is another solution that permits dynamic window resizing.
319 String cmdShellPtypipe
= "ptypipe /bin/bash --login";
321 // Spawn a shell and pass its I/O to the other constructor.
322 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
323 && (System
.getProperty("jexer.TTerminal.ptypipe").
327 spawnShell(cmdShellPtypipe
.split("\\s+"));
328 } else if (System
.getProperty("os.name").startsWith("Windows")) {
329 spawnShell(cmdShellWindows
.split("\\s+"));
330 } else if (System
.getProperty("os.name").startsWith("Mac")) {
331 spawnShell(cmdShellBSD
.split("\\s+"));
332 } else if (System
.getProperty("os.name").startsWith("Linux")) {
333 spawnShell(cmdShellGNU
.split("\\s+"));
335 // When all else fails, assume GNU.
336 spawnShell(cmdShellGNU
.split("\\s+"));
340 // ------------------------------------------------------------------------
341 // TScrollableWindow ------------------------------------------------------
342 // ------------------------------------------------------------------------
345 * Draw the display buffer.
349 int width
= getDisplayWidth();
350 boolean syncEmulator
= false;
351 if ((System
.currentTimeMillis() - lastUpdateTime
>= 25)
354 // Too much time has passed, draw it all.
356 } else if (emulator
.isReading() && (dirty
== false)) {
357 // Wait until the emulator has brought more data in.
358 syncEmulator
= false;
359 } else if (!emulator
.isReading() && (dirty
== true)) {
360 // The emulator won't receive more data, update the display.
364 if ((syncEmulator
== true)
365 || (scrollback
== null)
368 // We want to minimize the amount of time we have the emulator
369 // locked. Grab a copy of its display.
370 synchronized (emulator
) {
371 // Update the scroll bars
374 if ((scrollback
== null) || emulator
.isReading()) {
375 scrollback
= copyBuffer(emulator
.getScrollbackBuffer());
376 display
= copyBuffer(emulator
.getDisplayBuffer());
378 width
= emulator
.getWidth();
383 // Draw the box using my superclass
386 // Put together the visible rows
387 int visibleHeight
= getHeight() - 2;
388 int visibleBottom
= scrollback
.size() + display
.size()
389 + getVerticalValue();
390 assert (visibleBottom
>= 0);
392 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
393 int visibleTop
= visibleBottom
- visibleHeight
;
394 if (visibleTop
< 0) {
395 for (int i
= visibleTop
; i
< 0; i
++) {
396 preceedingBlankLines
.add(emulator
.getBlankDisplayLine());
400 assert (visibleTop
>= 0);
402 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
403 displayLines
.addAll(scrollback
);
404 displayLines
.addAll(display
);
406 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
407 visibleLines
.addAll(preceedingBlankLines
);
408 visibleLines
.addAll(displayLines
.subList(visibleTop
,
411 visibleHeight
-= visibleLines
.size();
412 assert (visibleHeight
>= 0);
414 // Now draw the emulator screen
416 for (DisplayLine line
: visibleLines
) {
417 int widthMax
= width
;
418 if (line
.isDoubleWidth()) {
421 if (widthMax
> getWidth() - 2) {
422 widthMax
= getWidth() - 2;
424 for (int i
= 0; i
< widthMax
; i
++) {
425 Cell ch
= line
.charAt(i
);
428 putCharXY(i
+ 1, row
, ch
);
432 Cell newCell
= new Cell(ch
);
433 boolean reverse
= line
.isReverseColor() ^ ch
.isReverse();
434 newCell
.setReverse(false);
436 if (ch
.getForeColorRGB() < 0) {
437 newCell
.setBackColor(ch
.getForeColor());
438 newCell
.setBackColorRGB(-1);
440 newCell
.setBackColorRGB(ch
.getForeColorRGB());
442 if (ch
.getBackColorRGB() < 0) {
443 newCell
.setForeColor(ch
.getBackColor());
444 newCell
.setForeColorRGB(-1);
446 newCell
.setForeColorRGB(ch
.getBackColorRGB());
449 if (line
.isDoubleWidth()) {
450 putDoubleWidthCharXY(line
, (i
* 2) + 1, row
, newCell
);
452 putCharXY(i
+ 1, row
, newCell
);
456 if (row
== getHeight() - 1) {
457 // Don't overwrite the box edge
461 CellAttributes background
= new CellAttributes();
462 // Fill in the blank lines on bottom
463 for (int i
= 0; i
< visibleHeight
; i
++) {
464 hLineXY(1, i
+ row
, getWidth() - 2, ' ', background
);
470 * Handle window close.
473 public void onClose() {
476 terminateShellChildProcess();
483 * Handle window/screen resize events.
485 * @param resize resize event
488 public void onResize(final TResizeEvent resize
) {
490 // Synchronize against the emulator so we don't stomp on its reader
492 synchronized (emulator
) {
494 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
495 // Resize the scroll bars
499 // Get out of scrollback
503 emulator
.setWidth(getWidth() - 2);
504 emulator
.setHeight(getHeight() - 2);
506 emulator
.writeRemote("\033[8;" + (getHeight() - 2) + ";" +
507 (getWidth() - 2) + "t");
512 } // synchronized (emulator)
516 * Resize scrollbars for a new width/height.
519 public void reflowData() {
521 // Synchronize against the emulator so we don't stomp on its reader
523 synchronized (emulator
) {
525 // Pull cursor information
528 // Vertical scrollbar
529 setTopValue(getHeight() - 2
530 - (emulator
.getScrollbackBuffer().size()
531 + emulator
.getDisplayBuffer().size()));
532 setVerticalBigChange(getHeight() - 2);
534 } // synchronized (emulator)
540 * @param keypress keystroke event
543 public void onKeypress(final TKeypressEvent keypress
) {
544 if (hideMouseWhenTyping
) {
545 typingHidMouse
= true;
548 // Scrollback up/down
549 if (keypress
.equals(kbShiftPgUp
)
550 || keypress
.equals(kbCtrlPgUp
)
551 || keypress
.equals(kbAltPgUp
)
553 bigVerticalDecrement();
556 if (keypress
.equals(kbShiftPgDn
)
557 || keypress
.equals(kbCtrlPgDn
)
558 || keypress
.equals(kbAltPgDn
)
560 bigVerticalIncrement();
564 if (emulator
.isReading()) {
565 // Get out of scrollback
567 emulator
.addUserEvent(keypress
);
569 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
570 // this is kBEnter then also send kbCtrlJ.
571 if (keypress
.equals(kbEnter
)) {
572 if (System
.getProperty("os.name").startsWith("Windows")
573 && (System
.getProperty("jexer.TTerminal.cmdHack",
574 "true").equals("true"))
576 emulator
.addUserEvent(new TKeypressEvent(kbCtrlJ
));
584 // Process is closed, honor "normal" TUI keystrokes
585 super.onKeypress(keypress
);
589 * Handle mouse press events.
591 * @param mouse mouse button press event
594 public void onMouseDown(final TMouseEvent mouse
) {
595 if (inWindowMove
|| inWindowResize
) {
596 // TWindow needs to deal with this.
597 super.onMouseDown(mouse
);
601 if (hideMouseWhenTyping
) {
602 typingHidMouse
= false;
605 // If the emulator is tracking mouse buttons, it needs to see wheel
607 if (emulator
.getMouseProtocol() == ECMA48
.MouseProtocol
.OFF
) {
608 if (mouse
.isMouseWheelUp()) {
612 if (mouse
.isMouseWheelDown()) {
617 if (mouseOnEmulator(mouse
)) {
618 mouse
.setX(mouse
.getX() - 1);
619 mouse
.setY(mouse
.getY() - 1);
620 emulator
.addUserEvent(mouse
);
625 // Emulator didn't consume it, pass it on
626 super.onMouseDown(mouse
);
630 * Handle mouse release events.
632 * @param mouse mouse button release event
635 public void onMouseUp(final TMouseEvent mouse
) {
636 if (inWindowMove
|| inWindowResize
) {
637 // TWindow needs to deal with this.
638 super.onMouseUp(mouse
);
642 if (hideMouseWhenTyping
) {
643 typingHidMouse
= false;
646 if (mouseOnEmulator(mouse
)) {
647 mouse
.setX(mouse
.getX() - 1);
648 mouse
.setY(mouse
.getY() - 1);
649 emulator
.addUserEvent(mouse
);
654 // Emulator didn't consume it, pass it on
655 super.onMouseUp(mouse
);
659 * Handle mouse motion events.
661 * @param mouse mouse motion event
664 public void onMouseMotion(final TMouseEvent mouse
) {
665 if (inWindowMove
|| inWindowResize
) {
666 // TWindow needs to deal with this.
667 super.onMouseMotion(mouse
);
671 if (hideMouseWhenTyping
) {
672 typingHidMouse
= false;
675 if (mouseOnEmulator(mouse
)) {
676 mouse
.setX(mouse
.getX() - 1);
677 mouse
.setY(mouse
.getY() - 1);
678 emulator
.addUserEvent(mouse
);
683 // Emulator didn't consume it, pass it on
684 super.onMouseMotion(mouse
);
687 // ------------------------------------------------------------------------
688 // TTerminalWindow --------------------------------------------------------
689 // ------------------------------------------------------------------------
692 * Returns true if this window does not want the application-wide mouse
693 * cursor drawn over it.
695 * @return true if this window does not want the application-wide mouse
696 * cursor drawn over it
699 public boolean hasHiddenMouse() {
700 return (super.hasHiddenMouse() || typingHidMouse
);
704 * Claim the keystrokes the emulator will need.
706 private void addShortcutKeys() {
707 addShortcutKeypress(kbCtrlA
);
708 addShortcutKeypress(kbCtrlB
);
709 addShortcutKeypress(kbCtrlC
);
710 addShortcutKeypress(kbCtrlD
);
711 addShortcutKeypress(kbCtrlE
);
712 addShortcutKeypress(kbCtrlF
);
713 addShortcutKeypress(kbCtrlG
);
714 addShortcutKeypress(kbCtrlH
);
715 addShortcutKeypress(kbCtrlU
);
716 addShortcutKeypress(kbCtrlJ
);
717 addShortcutKeypress(kbCtrlK
);
718 addShortcutKeypress(kbCtrlL
);
719 addShortcutKeypress(kbCtrlM
);
720 addShortcutKeypress(kbCtrlN
);
721 addShortcutKeypress(kbCtrlO
);
722 addShortcutKeypress(kbCtrlP
);
723 addShortcutKeypress(kbCtrlQ
);
724 addShortcutKeypress(kbCtrlR
);
725 addShortcutKeypress(kbCtrlS
);
726 addShortcutKeypress(kbCtrlT
);
727 addShortcutKeypress(kbCtrlU
);
728 addShortcutKeypress(kbCtrlV
);
729 addShortcutKeypress(kbCtrlW
);
730 addShortcutKeypress(kbCtrlX
);
731 addShortcutKeypress(kbCtrlY
);
732 addShortcutKeypress(kbCtrlZ
);
733 addShortcutKeypress(kbF1
);
734 addShortcutKeypress(kbF2
);
735 addShortcutKeypress(kbF3
);
736 addShortcutKeypress(kbF4
);
737 addShortcutKeypress(kbF5
);
738 addShortcutKeypress(kbF6
);
739 addShortcutKeypress(kbF7
);
740 addShortcutKeypress(kbF8
);
741 addShortcutKeypress(kbF9
);
742 addShortcutKeypress(kbF10
);
743 addShortcutKeypress(kbF11
);
744 addShortcutKeypress(kbF12
);
745 addShortcutKeypress(kbAltA
);
746 addShortcutKeypress(kbAltB
);
747 addShortcutKeypress(kbAltC
);
748 addShortcutKeypress(kbAltD
);
749 addShortcutKeypress(kbAltE
);
750 addShortcutKeypress(kbAltF
);
751 addShortcutKeypress(kbAltG
);
752 addShortcutKeypress(kbAltH
);
753 addShortcutKeypress(kbAltU
);
754 addShortcutKeypress(kbAltJ
);
755 addShortcutKeypress(kbAltK
);
756 addShortcutKeypress(kbAltL
);
757 addShortcutKeypress(kbAltM
);
758 addShortcutKeypress(kbAltN
);
759 addShortcutKeypress(kbAltO
);
760 addShortcutKeypress(kbAltP
);
761 addShortcutKeypress(kbAltQ
);
762 addShortcutKeypress(kbAltR
);
763 addShortcutKeypress(kbAltS
);
764 addShortcutKeypress(kbAltT
);
765 addShortcutKeypress(kbAltU
);
766 addShortcutKeypress(kbAltV
);
767 addShortcutKeypress(kbAltW
);
768 addShortcutKeypress(kbAltX
);
769 addShortcutKeypress(kbAltY
);
770 addShortcutKeypress(kbAltZ
);
774 * Convert a string array to a whitespace-separated string.
776 * @param array the string array
777 * @return a single string
779 private String
stringArrayToString(final String
[] array
) {
780 StringBuilder sb
= new StringBuilder(array
[0].length());
781 for (int i
= 0; i
< array
.length
; i
++) {
783 if (i
< array
.length
- 1) {
787 return sb
.toString();
793 * @param command the command line to execute
795 private void spawnShell(final String
[] command
) {
798 System.err.printf("spawnShell(): '%s'\n",
799 stringArrayToString(command));
802 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
806 ECMA48
.DeviceType deviceType
= ECMA48
.DeviceType
.XTERM
;
809 ProcessBuilder pb
= new ProcessBuilder(command
);
810 Map
<String
, String
> env
= pb
.environment();
811 env
.put("TERM", ECMA48
.deviceTypeTerm(deviceType
));
812 env
.put("LANG", ECMA48
.deviceTypeLang(deviceType
, "en"));
813 env
.put("COLUMNS", "80");
814 env
.put("LINES", "24");
815 pb
.redirectErrorStream(true);
817 emulator
= new ECMA48(deviceType
, shell
.getInputStream(),
818 shell
.getOutputStream(), this);
819 } catch (IOException e
) {
820 messageBox(i18n
.getString("errorLaunchingShellTitle"),
821 MessageFormat
.format(i18n
.getString("errorLaunchingShellText"),
825 // Setup the scroll bars
826 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
829 // Claim the keystrokes the emulator will need.
833 newStatusBar(i18n
.getString("statusBarRunning"));
835 // Pass the correct text cell width/height to the emulator
836 emulator
.setTextWidth(getScreen().getTextWidth());
837 emulator
.setTextHeight(getScreen().getTextHeight());
839 // Hide mouse when typing option
840 if (System
.getProperty("jexer.TTerminal.hideMouseWhenTyping",
841 "true").equals("false")) {
843 hideMouseWhenTyping
= false;
848 * Terminate the child of the 'script' process used on POSIX. This may
851 private void terminateShellChildProcess() {
853 if (shell
.getClass().getName().equals("java.lang.UNIXProcess")) {
854 /* get the PID on unix/linux systems */
856 Field field
= shell
.getClass().getDeclaredField("pid");
857 field
.setAccessible(true);
858 pid
= field
.getInt(shell
);
859 } catch (Throwable e
) {
860 // SQUASH, this didn't work. Just bail out quietly.
865 // shell.destroy() works successfully at killing this side of
866 // 'script'. But we need to make sure the other side (child
867 // process) is also killed.
868 String
[] cmdKillIt
= {
869 "pkill", "-P", Integer
.toString(pid
)
872 Runtime
.getRuntime().exec(cmdKillIt
);
873 } catch (Throwable e
) {
874 // SQUASH, this didn't work. Just bail out quietly.
881 * Hook for subclasses to be notified of the shell termination.
883 public void onShellExit() {
887 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
891 * Copy out variables from the emulator that TTerminal has to expose on
894 private void readEmulatorState() {
895 // Synchronize against the emulator so we don't stomp on its reader
897 synchronized (emulator
) {
898 setHiddenMouse(emulator
.hasHiddenMousePointer());
900 setCursorX(emulator
.getCursorX() + 1);
901 setCursorY(emulator
.getCursorY() + 1
902 + (getHeight() - 2 - emulator
.getHeight())
903 - getVerticalValue());
904 setCursorVisible(emulator
.isCursorVisible());
905 if (getCursorX() > getWidth() - 2) {
906 setCursorVisible(false);
908 if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
909 setCursorVisible(false);
911 if (emulator
.getScreenTitle().length() > 0) {
912 // Only update the title if the shell is still alive
914 setTitle(emulator
.getScreenTitle());
918 // Check to see if the shell has died.
919 if (!emulator
.isReading() && (shell
!= null)) {
921 int rc
= shell
.exitValue();
922 // The emulator exited on its own, all is fine
923 setTitle(MessageFormat
.format(i18n
.
924 getString("windowTitleCompleted"), getTitle(), rc
));
927 clearShortcutKeypresses();
928 statusBar
.setText(MessageFormat
.format(i18n
.
929 getString("statusBarCompleted"), rc
));
931 } catch (IllegalThreadStateException e
) {
932 // The emulator thread has exited, but the shell Process
933 // hasn't figured that out yet. Do nothing, we will see
934 // this in a future tick.
936 } else if (emulator
.isReading() && (shell
!= null)) {
937 // The shell might be dead, let's check
939 int rc
= shell
.exitValue();
940 // If we got here, the shell died.
941 setTitle(MessageFormat
.format(i18n
.
942 getString("windowTitleCompleted"), getTitle(), rc
));
945 clearShortcutKeypresses();
946 statusBar
.setText(MessageFormat
.format(i18n
.
947 getString("statusBarCompleted"), rc
));
949 } catch (IllegalThreadStateException e
) {
950 // The shell is still running, do nothing.
954 } // synchronized (emulator)
958 * Check if a mouse press/release/motion event coordinate is over the
961 * @param mouse a mouse-based event
962 * @return whether or not the mouse is on the emulator
964 private boolean mouseOnEmulator(final TMouseEvent mouse
) {
966 if (!emulator
.isReading()) {
970 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
971 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
972 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
973 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
981 * Copy a display buffer.
983 * @param buffer the buffer to copy
984 * @return a deep copy of the buffer's data
986 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
987 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
988 for (DisplayLine line
: buffer
) {
989 result
.add(new DisplayLine(line
));
995 * Draw glyphs for a double-width or double-height VT100 cell to two
998 * @param line the line this VT100 cell is in
999 * @param x the X position to draw the left half to
1000 * @param y the Y position to draw to
1001 * @param cell the cell to draw
1003 private void putDoubleWidthCharXY(final DisplayLine line
, final int x
,
1004 final int y
, final Cell cell
) {
1006 int textWidth
= getScreen().getTextWidth();
1007 int textHeight
= getScreen().getTextHeight();
1008 boolean cursorBlinkVisible
= true;
1010 if (getScreen() instanceof SwingTerminal
) {
1011 SwingTerminal terminal
= (SwingTerminal
) getScreen();
1012 cursorBlinkVisible
= terminal
.getCursorBlinkVisible();
1013 } else if (getScreen() instanceof ECMA48Terminal
) {
1014 ECMA48Terminal terminal
= (ECMA48Terminal
) getScreen();
1016 if (!terminal
.hasSixel()) {
1017 // The backend does not have sixel support, draw this as text
1019 putCharXY(x
, y
, cell
);
1020 putCharXY(x
+ 1, y
, ' ', cell
);
1023 cursorBlinkVisible
= blinkState
;
1025 // We don't know how to dray glyphs to this screen, draw them as
1026 // text and bail out.
1027 putCharXY(x
, y
, cell
);
1028 putCharXY(x
+ 1, y
, ' ', cell
);
1032 if ((textWidth
!= lastTextWidth
) || (textHeight
!= lastTextHeight
)) {
1033 // Screen size has changed, reset the font.
1034 setupFont(textHeight
);
1035 lastTextWidth
= textWidth
;
1036 lastTextHeight
= textHeight
;
1038 assert (doubleFont
!= null);
1040 BufferedImage image
;
1041 if (line
.getDoubleHeight() == 1) {
1042 // Double-height top half: don't draw the underline.
1043 Cell newCell
= new Cell(cell
);
1044 newCell
.setUnderline(false);
1045 image
= doubleFont
.getImage(newCell
, textWidth
* 2, textHeight
* 2,
1046 cursorBlinkVisible
);
1048 image
= doubleFont
.getImage(cell
, textWidth
* 2, textHeight
* 2,
1049 cursorBlinkVisible
);
1052 // Now that we have the double-wide glyph drawn, copy the right
1053 // pieces of it to the cells.
1054 Cell left
= new Cell(cell
);
1055 Cell right
= new Cell(cell
);
1057 BufferedImage leftImage
= null;
1058 BufferedImage rightImage
= null;
1060 System.err.println("image " + image + " textWidth " + textWidth +
1061 " textHeight " + textHeight);
1064 switch (line
.getDoubleHeight()) {
1066 // Top half double height
1067 leftImage
= image
.getSubimage(0, 0, textWidth
, textHeight
);
1068 rightImage
= image
.getSubimage(textWidth
, 0, textWidth
, textHeight
);
1071 // Bottom half double height
1072 leftImage
= image
.getSubimage(0, textHeight
, textWidth
, textHeight
);
1073 rightImage
= image
.getSubimage(textWidth
, textHeight
,
1074 textWidth
, textHeight
);
1077 // Either single height double-width, or error fallback
1078 BufferedImage wideImage
= new BufferedImage(textWidth
* 2,
1079 textHeight
, BufferedImage
.TYPE_INT_ARGB
);
1080 Graphics2D grWide
= wideImage
.createGraphics();
1081 grWide
.drawImage(image
, 0, 0, wideImage
.getWidth(),
1082 wideImage
.getHeight(), null);
1084 leftImage
= wideImage
.getSubimage(0, 0, textWidth
, textHeight
);
1085 rightImage
= wideImage
.getSubimage(textWidth
, 0, textWidth
,
1089 left
.setImage(leftImage
);
1090 right
.setImage(rightImage
);
1091 // Since we have image data, ditch the character here. Otherwise, a
1092 // drawBoxShadow() over the terminal window will show the characters
1093 // which looks wrong.
1096 putCharXY(x
, y
, left
);
1097 putCharXY(x
+ 1, y
, right
);
1101 * Set up the double-width font.
1103 * @param fontSize the size of font to request for the single-width font.
1104 * The double-width font will be 2x this value.
1106 private void setupFont(final int fontSize
) {
1107 doubleFont
= GlyphMaker
.getInstance(fontSize
* 2);
1109 // Special case: the ECMA48 backend needs to have a timer to drive
1111 if (getScreen() instanceof jexer
.backend
.ECMA48Terminal
) {
1113 // Blink every 500 millis.
1115 getApplication().addTimer(millis
, true,
1118 blinkState
= !blinkState
;
1119 getApplication().doRepaint();
1128 // ------------------------------------------------------------------------
1129 // DisplayListener --------------------------------------------------------
1130 // ------------------------------------------------------------------------
1133 * Called by emulator when fresh data has come in.
1135 public void displayChanged() {
1137 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
1141 * Function to call to obtain the display width.
1143 * @return the number of columns in the display
1145 public int getDisplayWidth() {
1147 return getWidth() - 2;
1153 * Function to call to obtain the display height.
1155 * @return the number of rows in the display
1157 public int getDisplayHeight() {
1159 return getHeight() - 2;