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;
126 // ------------------------------------------------------------------------
127 // Constructors -----------------------------------------------------------
128 // ------------------------------------------------------------------------
131 * Public constructor spawns a custom command line.
133 * @param application TApplication that manages this window
134 * @param x column relative to parent
135 * @param y row relative to parent
136 * @param commandLine the command line to execute
138 public TTerminalWindow(final TApplication application
, final int x
,
139 final int y
, final String commandLine
) {
141 this(application
, x
, y
, RESIZABLE
, commandLine
.split("\\s+"),
142 System
.getProperty("jexer.TTerminal.closeOnExit",
143 "false").equals("true"));
147 * Public constructor spawns a custom command line.
149 * @param application TApplication that manages this window
150 * @param x column relative to parent
151 * @param y row relative to parent
152 * @param commandLine the command line to execute
153 * @param closeOnExit if true, close the window when the command exits
155 public TTerminalWindow(final TApplication application
, final int x
,
156 final int y
, final String commandLine
, final boolean closeOnExit
) {
158 this(application
, x
, y
, RESIZABLE
, commandLine
.split("\\s+"),
163 * Public constructor spawns a custom command line.
165 * @param application TApplication that manages this window
166 * @param x column relative to parent
167 * @param y row relative to parent
168 * @param flags mask of CENTERED, MODAL, or RESIZABLE
169 * @param command the command line to execute
171 public TTerminalWindow(final TApplication application
, final int x
,
172 final int y
, final int flags
, final String
[] command
) {
174 this(application
, x
, y
, flags
, command
,
175 System
.getProperty("jexer.TTerminal.closeOnExit",
176 "false").equals("true"));
180 * Public constructor spawns a custom command line.
182 * @param application TApplication that manages this window
183 * @param x column relative to parent
184 * @param y row relative to parent
185 * @param flags mask of CENTERED, MODAL, or RESIZABLE
186 * @param command the command line to execute
187 * @param closeOnExit if true, close the window when the command exits
189 public TTerminalWindow(final TApplication application
, final int x
,
190 final int y
, final int flags
, final String
[] command
,
191 final boolean closeOnExit
) {
193 super(application
, i18n
.getString("windowTitle"), x
, y
,
194 80 + 2, 24 + 2, flags
);
196 this.closeOnExit
= closeOnExit
;
198 String
[] fullCommand
;
200 // Spawn a shell and pass its I/O to the other constructor.
201 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
202 && (System
.getProperty("jexer.TTerminal.ptypipe").
206 fullCommand
= new String
[command
.length
+ 1];
207 fullCommand
[0] = "ptypipe";
208 System
.arraycopy(command
, 0, fullCommand
, 1, command
.length
);
209 } else if (System
.getProperty("os.name").startsWith("Windows")) {
210 fullCommand
= new String
[3];
211 fullCommand
[0] = "cmd";
212 fullCommand
[1] = "/c";
213 fullCommand
[2] = stringArrayToString(command
);
214 } else if (System
.getProperty("os.name").startsWith("Mac")) {
215 fullCommand
= new String
[6];
216 fullCommand
[0] = "script";
217 fullCommand
[1] = "-q";
218 fullCommand
[2] = "-F";
219 fullCommand
[3] = "/dev/null";
220 fullCommand
[4] = "-c";
221 fullCommand
[5] = stringArrayToString(command
);
223 // Default: behave like Linux
224 fullCommand
= new String
[5];
225 fullCommand
[0] = "script";
226 fullCommand
[1] = "-fqe";
227 fullCommand
[2] = "/dev/null";
228 fullCommand
[3] = "-c";
229 fullCommand
[4] = stringArrayToString(command
);
231 spawnShell(fullCommand
);
235 * Public constructor spawns a shell.
237 * @param application TApplication that manages this window
238 * @param x column relative to parent
239 * @param y row relative to parent
240 * @param flags mask of CENTERED, MODAL, or RESIZABLE
242 public TTerminalWindow(final TApplication application
, final int x
,
243 final int y
, final int flags
) {
245 this(application
, x
, y
, flags
,
246 System
.getProperty("jexer.TTerminal.closeOnExit",
247 "false").equals("true"));
252 * Public constructor spawns a shell.
254 * @param application TApplication that manages this window
255 * @param x column relative to parent
256 * @param y row relative to parent
257 * @param flags mask of CENTERED, MODAL, or RESIZABLE
258 * @param closeOnExit if true, close the window when the shell exits
260 public TTerminalWindow(final TApplication application
, final int x
,
261 final int y
, final int flags
, final boolean closeOnExit
) {
263 super(application
, i18n
.getString("windowTitle"), x
, y
,
264 80 + 2, 24 + 2, flags
);
266 this.closeOnExit
= closeOnExit
;
268 String cmdShellWindows
= "cmd.exe";
270 // You cannot run a login shell in a bare Process interactively, due
271 // to libc's behavior of buffering when stdin/stdout aren't a tty.
272 // Use 'script' instead to run a shell in a pty. And because BSD and
273 // GNU differ on the '-f' vs '-F' flags, we need two different
275 String cmdShellGNU
= "script -fqe /dev/null";
276 String cmdShellBSD
= "script -q -F /dev/null";
278 // ptypipe is another solution that permits dynamic window resizing.
279 String cmdShellPtypipe
= "ptypipe /bin/bash --login";
281 // Spawn a shell and pass its I/O to the other constructor.
282 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
283 && (System
.getProperty("jexer.TTerminal.ptypipe").
287 spawnShell(cmdShellPtypipe
.split("\\s+"));
288 } else if (System
.getProperty("os.name").startsWith("Windows")) {
289 spawnShell(cmdShellWindows
.split("\\s+"));
290 } else if (System
.getProperty("os.name").startsWith("Mac")) {
291 spawnShell(cmdShellBSD
.split("\\s+"));
292 } else if (System
.getProperty("os.name").startsWith("Linux")) {
293 spawnShell(cmdShellGNU
.split("\\s+"));
295 // When all else fails, assume GNU.
296 spawnShell(cmdShellGNU
.split("\\s+"));
300 // ------------------------------------------------------------------------
301 // TScrollableWindow ------------------------------------------------------
302 // ------------------------------------------------------------------------
305 * Draw the display buffer.
309 // Synchronize against the emulator so we don't stomp on its reader
311 synchronized (emulator
) {
313 // Update the scroll bars
316 // Draw the box using my superclass
319 List
<DisplayLine
> scrollback
= emulator
.getScrollbackBuffer();
320 List
<DisplayLine
> display
= emulator
.getDisplayBuffer();
322 // Put together the visible rows
323 int visibleHeight
= getHeight() - 2;
324 int visibleBottom
= scrollback
.size() + display
.size()
325 + getVerticalValue();
326 assert (visibleBottom
>= 0);
328 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
329 int visibleTop
= visibleBottom
- visibleHeight
;
330 if (visibleTop
< 0) {
331 for (int i
= visibleTop
; i
< 0; i
++) {
332 preceedingBlankLines
.add(emulator
.getBlankDisplayLine());
336 assert (visibleTop
>= 0);
338 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
339 displayLines
.addAll(scrollback
);
340 displayLines
.addAll(display
);
342 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
343 visibleLines
.addAll(preceedingBlankLines
);
344 visibleLines
.addAll(displayLines
.subList(visibleTop
,
347 visibleHeight
-= visibleLines
.size();
348 assert (visibleHeight
>= 0);
350 // Now draw the emulator screen
352 for (DisplayLine line
: visibleLines
) {
353 int widthMax
= emulator
.getWidth();
354 if (line
.isDoubleWidth()) {
357 if (widthMax
> getWidth() - 2) {
358 widthMax
= getWidth() - 2;
360 for (int i
= 0; i
< widthMax
; i
++) {
361 Cell ch
= line
.charAt(i
);
364 putCharXY(i
+ 1, row
, ch
);
368 Cell newCell
= new Cell();
370 boolean reverse
= line
.isReverseColor() ^ ch
.isReverse();
371 newCell
.setReverse(false);
373 if (ch
.getForeColorRGB() < 0) {
374 newCell
.setBackColor(ch
.getForeColor());
375 newCell
.setBackColorRGB(-1);
377 newCell
.setBackColorRGB(ch
.getForeColorRGB());
379 if (ch
.getBackColorRGB() < 0) {
380 newCell
.setForeColor(ch
.getBackColor());
381 newCell
.setForeColorRGB(-1);
383 newCell
.setForeColorRGB(ch
.getBackColorRGB());
386 if (line
.isDoubleWidth()) {
387 putDoubleWidthCharXY(line
, (i
* 2) + 1, row
, newCell
);
389 putCharXY(i
+ 1, row
, newCell
);
393 if (row
== getHeight() - 1) {
394 // Don't overwrite the box edge
398 CellAttributes background
= new CellAttributes();
399 // Fill in the blank lines on bottom
400 for (int i
= 0; i
< visibleHeight
; i
++) {
401 hLineXY(1, i
+ row
, getWidth() - 2, ' ', background
);
404 } // synchronized (emulator)
409 * Handle window close.
412 public void onClose() {
415 terminateShellChildProcess();
422 * Handle window/screen resize events.
424 * @param resize resize event
427 public void onResize(final TResizeEvent resize
) {
429 // Synchronize against the emulator so we don't stomp on its reader
431 synchronized (emulator
) {
433 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
434 // Resize the scroll bars
438 // Get out of scrollback
442 emulator
.setWidth(getWidth() - 2);
443 emulator
.setHeight(getHeight() - 2);
445 emulator
.writeRemote("\033[8;" + (getHeight() - 2) + ";" +
446 (getWidth() - 2) + "t");
451 } // synchronized (emulator)
455 * Resize scrollbars for a new width/height.
458 public void reflowData() {
460 // Synchronize against the emulator so we don't stomp on its reader
462 synchronized (emulator
) {
464 // Pull cursor information
467 // Vertical scrollbar
468 setTopValue(getHeight() - 2
469 - (emulator
.getScrollbackBuffer().size()
470 + emulator
.getDisplayBuffer().size()));
471 setVerticalBigChange(getHeight() - 2);
473 } // synchronized (emulator)
479 * @param keypress keystroke event
482 public void onKeypress(final TKeypressEvent keypress
) {
484 // Scrollback up/down
485 if (keypress
.equals(kbShiftPgUp
)
486 || keypress
.equals(kbCtrlPgUp
)
487 || keypress
.equals(kbAltPgUp
)
489 bigVerticalDecrement();
492 if (keypress
.equals(kbShiftPgDn
)
493 || keypress
.equals(kbCtrlPgDn
)
494 || keypress
.equals(kbAltPgDn
)
496 bigVerticalIncrement();
500 // Synchronize against the emulator so we don't stomp on its reader
502 synchronized (emulator
) {
503 if (emulator
.isReading()) {
504 // Get out of scrollback
506 emulator
.keypress(keypress
.getKey());
508 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
509 // this is kBEnter then also send kbCtrlJ.
510 if (System
.getProperty("os.name").startsWith("Windows")) {
511 if (keypress
.equals(kbEnter
)) {
512 emulator
.keypress(kbCtrlJ
);
521 // Process is closed, honor "normal" TUI keystrokes
522 super.onKeypress(keypress
);
526 * Handle mouse press events.
528 * @param mouse mouse button press event
531 public void onMouseDown(final TMouseEvent mouse
) {
532 if (inWindowMove
|| inWindowResize
) {
533 // TWindow needs to deal with this.
534 super.onMouseDown(mouse
);
538 // If the emulator is tracking mouse buttons, it needs to see wheel
540 if (emulator
.getMouseProtocol() == ECMA48
.MouseProtocol
.OFF
) {
541 if (mouse
.isMouseWheelUp()) {
545 if (mouse
.isMouseWheelDown()) {
550 if (mouseOnEmulator(mouse
)) {
551 synchronized (emulator
) {
552 mouse
.setX(mouse
.getX() - 1);
553 mouse
.setY(mouse
.getY() - 1);
554 emulator
.mouse(mouse
);
560 // Emulator didn't consume it, pass it on
561 super.onMouseDown(mouse
);
565 * Handle mouse release events.
567 * @param mouse mouse button release event
570 public void onMouseUp(final TMouseEvent mouse
) {
571 if (inWindowMove
|| inWindowResize
) {
572 // TWindow needs to deal with this.
573 super.onMouseUp(mouse
);
577 if (mouseOnEmulator(mouse
)) {
578 synchronized (emulator
) {
579 mouse
.setX(mouse
.getX() - 1);
580 mouse
.setY(mouse
.getY() - 1);
581 emulator
.mouse(mouse
);
587 // Emulator didn't consume it, pass it on
588 super.onMouseUp(mouse
);
592 * Handle mouse motion events.
594 * @param mouse mouse motion event
597 public void onMouseMotion(final TMouseEvent mouse
) {
598 if (inWindowMove
|| inWindowResize
) {
599 // TWindow needs to deal with this.
600 super.onMouseMotion(mouse
);
604 if (mouseOnEmulator(mouse
)) {
605 synchronized (emulator
) {
606 mouse
.setX(mouse
.getX() - 1);
607 mouse
.setY(mouse
.getY() - 1);
608 emulator
.mouse(mouse
);
614 // Emulator didn't consume it, pass it on
615 super.onMouseMotion(mouse
);
618 // ------------------------------------------------------------------------
619 // TTerminalWindow --------------------------------------------------------
620 // ------------------------------------------------------------------------
623 * Claim the keystrokes the emulator will need.
625 private void addShortcutKeys() {
626 addShortcutKeypress(kbCtrlA
);
627 addShortcutKeypress(kbCtrlB
);
628 addShortcutKeypress(kbCtrlC
);
629 addShortcutKeypress(kbCtrlD
);
630 addShortcutKeypress(kbCtrlE
);
631 addShortcutKeypress(kbCtrlF
);
632 addShortcutKeypress(kbCtrlG
);
633 addShortcutKeypress(kbCtrlH
);
634 addShortcutKeypress(kbCtrlU
);
635 addShortcutKeypress(kbCtrlJ
);
636 addShortcutKeypress(kbCtrlK
);
637 addShortcutKeypress(kbCtrlL
);
638 addShortcutKeypress(kbCtrlM
);
639 addShortcutKeypress(kbCtrlN
);
640 addShortcutKeypress(kbCtrlO
);
641 addShortcutKeypress(kbCtrlP
);
642 addShortcutKeypress(kbCtrlQ
);
643 addShortcutKeypress(kbCtrlR
);
644 addShortcutKeypress(kbCtrlS
);
645 addShortcutKeypress(kbCtrlT
);
646 addShortcutKeypress(kbCtrlU
);
647 addShortcutKeypress(kbCtrlV
);
648 addShortcutKeypress(kbCtrlW
);
649 addShortcutKeypress(kbCtrlX
);
650 addShortcutKeypress(kbCtrlY
);
651 addShortcutKeypress(kbCtrlZ
);
652 addShortcutKeypress(kbF1
);
653 addShortcutKeypress(kbF2
);
654 addShortcutKeypress(kbF3
);
655 addShortcutKeypress(kbF4
);
656 addShortcutKeypress(kbF5
);
657 addShortcutKeypress(kbF6
);
658 addShortcutKeypress(kbF7
);
659 addShortcutKeypress(kbF8
);
660 addShortcutKeypress(kbF9
);
661 addShortcutKeypress(kbF10
);
662 addShortcutKeypress(kbF11
);
663 addShortcutKeypress(kbF12
);
664 addShortcutKeypress(kbAltA
);
665 addShortcutKeypress(kbAltB
);
666 addShortcutKeypress(kbAltC
);
667 addShortcutKeypress(kbAltD
);
668 addShortcutKeypress(kbAltE
);
669 addShortcutKeypress(kbAltF
);
670 addShortcutKeypress(kbAltG
);
671 addShortcutKeypress(kbAltH
);
672 addShortcutKeypress(kbAltU
);
673 addShortcutKeypress(kbAltJ
);
674 addShortcutKeypress(kbAltK
);
675 addShortcutKeypress(kbAltL
);
676 addShortcutKeypress(kbAltM
);
677 addShortcutKeypress(kbAltN
);
678 addShortcutKeypress(kbAltO
);
679 addShortcutKeypress(kbAltP
);
680 addShortcutKeypress(kbAltQ
);
681 addShortcutKeypress(kbAltR
);
682 addShortcutKeypress(kbAltS
);
683 addShortcutKeypress(kbAltT
);
684 addShortcutKeypress(kbAltU
);
685 addShortcutKeypress(kbAltV
);
686 addShortcutKeypress(kbAltW
);
687 addShortcutKeypress(kbAltX
);
688 addShortcutKeypress(kbAltY
);
689 addShortcutKeypress(kbAltZ
);
693 * Convert a string array to a whitespace-separated string.
695 * @param array the string array
696 * @return a single string
698 private String
stringArrayToString(final String
[] array
) {
699 StringBuilder sb
= new StringBuilder(array
[0].length());
700 for (int i
= 0; i
< array
.length
; i
++) {
702 if (i
< array
.length
- 1) {
706 return sb
.toString();
712 * @param command the command line to execute
714 private void spawnShell(final String
[] command
) {
717 System.err.printf("spawnShell(): '%s'\n",
718 stringArrayToString(command));
721 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
725 ECMA48
.DeviceType deviceType
= ECMA48
.DeviceType
.XTERM
;
728 ProcessBuilder pb
= new ProcessBuilder(command
);
729 Map
<String
, String
> env
= pb
.environment();
730 env
.put("TERM", ECMA48
.deviceTypeTerm(deviceType
));
731 env
.put("LANG", ECMA48
.deviceTypeLang(deviceType
, "en"));
732 env
.put("COLUMNS", "80");
733 env
.put("LINES", "24");
734 pb
.redirectErrorStream(true);
736 emulator
= new ECMA48(deviceType
, shell
.getInputStream(),
737 shell
.getOutputStream(), this);
738 } catch (IOException e
) {
739 messageBox(i18n
.getString("errorLaunchingShellTitle"),
740 MessageFormat
.format(i18n
.getString("errorLaunchingShellText"),
744 // Setup the scroll bars
745 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
748 // Claim the keystrokes the emulator will need.
752 newStatusBar(i18n
.getString("statusBarRunning"));
754 // Pass the correct text cell width/height to the emulator
755 emulator
.setTextWidth(getScreen().getTextWidth());
756 emulator
.setTextHeight(getScreen().getTextHeight());
760 * Terminate the child of the 'script' process used on POSIX. This may
763 private void terminateShellChildProcess() {
765 if (shell
.getClass().getName().equals("java.lang.UNIXProcess")) {
766 /* get the PID on unix/linux systems */
768 Field field
= shell
.getClass().getDeclaredField("pid");
769 field
.setAccessible(true);
770 pid
= field
.getInt(shell
);
771 } catch (Throwable e
) {
772 // SQUASH, this didn't work. Just bail out quietly.
777 // shell.destroy() works successfully at killing this side of
778 // 'script'. But we need to make sure the other side (child
779 // process) is also killed.
780 String
[] cmdKillIt
= {
781 "pkill", "-P", Integer
.toString(pid
)
784 Runtime
.getRuntime().exec(cmdKillIt
);
785 } catch (Throwable e
) {
786 // SQUASH, this didn't work. Just bail out quietly.
793 * Called by emulator when fresh data has come in.
795 public void displayChanged() {
796 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
800 * Function to call to obtain the display width.
802 * @return the number of columns in the display
804 public int getDisplayWidth() {
806 return getWidth() - 2;
812 * Function to call to obtain the display height.
814 * @return the number of rows in the display
816 public int getDisplayHeight() {
818 return getHeight() - 2;
824 * Hook for subclasses to be notified of the shell termination.
826 public void onShellExit() {
830 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
834 * Copy out variables from the emulator that TTerminal has to expose on
837 private void readEmulatorState() {
838 // Synchronize against the emulator so we don't stomp on its reader
840 synchronized (emulator
) {
841 setHiddenMouse(emulator
.hasHiddenMousePointer());
843 setCursorX(emulator
.getCursorX() + 1);
844 setCursorY(emulator
.getCursorY() + 1
845 + (getHeight() - 2 - emulator
.getHeight())
846 - getVerticalValue());
847 setCursorVisible(emulator
.isCursorVisible());
848 if (getCursorX() > getWidth() - 2) {
849 setCursorVisible(false);
851 if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
852 setCursorVisible(false);
854 if (emulator
.getScreenTitle().length() > 0) {
855 // Only update the title if the shell is still alive
857 setTitle(emulator
.getScreenTitle());
861 // Check to see if the shell has died.
862 if (!emulator
.isReading() && (shell
!= null)) {
864 int rc
= shell
.exitValue();
865 // The emulator exited on its own, all is fine
866 setTitle(MessageFormat
.format(i18n
.
867 getString("windowTitleCompleted"), getTitle(), rc
));
870 clearShortcutKeypresses();
871 statusBar
.setText(MessageFormat
.format(i18n
.
872 getString("statusBarCompleted"), rc
));
874 } catch (IllegalThreadStateException e
) {
875 // The emulator thread has exited, but the shell Process
876 // hasn't figured that out yet. Do nothing, we will see
877 // this in a future tick.
879 } else if (emulator
.isReading() && (shell
!= null)) {
880 // The shell might be dead, let's check
882 int rc
= shell
.exitValue();
883 // If we got here, the shell died.
884 setTitle(MessageFormat
.format(i18n
.
885 getString("windowTitleCompleted"), getTitle(), rc
));
888 clearShortcutKeypresses();
889 statusBar
.setText(MessageFormat
.format(i18n
.
890 getString("statusBarCompleted"), rc
));
892 } catch (IllegalThreadStateException e
) {
893 // The shell is still running, do nothing.
897 } // synchronized (emulator)
901 * Check if a mouse press/release/motion event coordinate is over the
904 * @param mouse a mouse-based event
905 * @return whether or not the mouse is on the emulator
907 private boolean mouseOnEmulator(final TMouseEvent mouse
) {
909 synchronized (emulator
) {
910 if (!emulator
.isReading()) {
915 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
916 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
917 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
918 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
926 * Draw glyphs for a double-width or double-height VT100 cell to two
929 * @param line the line this VT100 cell is in
930 * @param x the X position to draw the left half to
931 * @param y the Y position to draw to
932 * @param cell the cell to draw
934 private void putDoubleWidthCharXY(final DisplayLine line
, final int x
,
935 final int y
, final Cell cell
) {
937 int textWidth
= getScreen().getTextWidth();
938 int textHeight
= getScreen().getTextHeight();
939 boolean cursorBlinkVisible
= true;
941 if (getScreen() instanceof SwingTerminal
) {
942 SwingTerminal terminal
= (SwingTerminal
) getScreen();
943 cursorBlinkVisible
= terminal
.getCursorBlinkVisible();
944 } else if (getScreen() instanceof ECMA48Terminal
) {
945 ECMA48Terminal terminal
= (ECMA48Terminal
) getScreen();
947 if (!terminal
.hasSixel()) {
948 // The backend does not have sixel support, draw this as text
950 putCharXY(x
, y
, cell
);
951 putCharXY(x
+ 1, y
, ' ', cell
);
954 cursorBlinkVisible
= blinkState
;
956 // We don't know how to dray glyphs to this screen, draw them as
957 // text and bail out.
958 putCharXY(x
, y
, cell
);
959 putCharXY(x
+ 1, y
, ' ', cell
);
963 if ((textWidth
!= lastTextWidth
) || (textHeight
!= lastTextHeight
)) {
964 // Screen size has changed, reset the font.
965 setupFont(textHeight
);
966 lastTextWidth
= textWidth
;
967 lastTextHeight
= textHeight
;
969 assert (doubleFont
!= null);
972 if (line
.getDoubleHeight() == 1) {
973 // Double-height top half: don't draw the underline.
974 Cell newCell
= new Cell();
976 newCell
.setUnderline(false);
977 image
= doubleFont
.getImage(newCell
, textWidth
* 2, textHeight
* 2,
980 image
= doubleFont
.getImage(cell
, textWidth
* 2, textHeight
* 2,
984 // Now that we have the double-wide glyph drawn, copy the right
985 // pieces of it to the cells.
986 Cell left
= new Cell();
987 Cell right
= new Cell();
991 BufferedImage leftImage
= null;
992 BufferedImage rightImage
= null;
994 System.err.println("image " + image + " textWidth " + textWidth +
995 " textHeight " + textHeight);
998 switch (line
.getDoubleHeight()) {
1000 // Top half double height
1001 leftImage
= image
.getSubimage(0, 0, textWidth
, textHeight
);
1002 rightImage
= image
.getSubimage(textWidth
, 0, textWidth
, textHeight
);
1005 // Bottom half double height
1006 leftImage
= image
.getSubimage(0, textHeight
, textWidth
, textHeight
);
1007 rightImage
= image
.getSubimage(textWidth
, textHeight
,
1008 textWidth
, textHeight
);
1011 // Either single height double-width, or error fallback
1012 BufferedImage wideImage
= new BufferedImage(textWidth
* 2,
1013 textHeight
, BufferedImage
.TYPE_INT_ARGB
);
1014 Graphics2D grWide
= wideImage
.createGraphics();
1015 grWide
.drawImage(image
, 0, 0, wideImage
.getWidth(),
1016 wideImage
.getHeight(), null);
1018 leftImage
= wideImage
.getSubimage(0, 0, textWidth
, textHeight
);
1019 rightImage
= wideImage
.getSubimage(textWidth
, 0, textWidth
,
1023 left
.setImage(leftImage
);
1024 right
.setImage(rightImage
);
1025 // Since we have image data, ditch the character here. Otherwise, a
1026 // drawBoxShadow() over the terminal window will show the characters
1027 // which looks wrong.
1030 putCharXY(x
, y
, left
);
1031 putCharXY(x
+ 1, y
, right
);
1035 * Set up the double-width font.
1037 * @param fontSize the size of font to request for the single-width font.
1038 * The double-width font will be 2x this value.
1040 private void setupFont(final int fontSize
) {
1041 doubleFont
= GlyphMaker
.getInstance(fontSize
* 2);
1043 // Special case: the ECMA48 backend needs to have a timer to drive
1045 if (getScreen() instanceof jexer
.backend
.ECMA48Terminal
) {
1047 // Blink every 500 millis.
1049 getApplication().addTimer(millis
, true,
1052 blinkState
= !blinkState
;
1053 getApplication().doRepaint();