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
);
362 Cell newCell
= new Cell();
364 boolean reverse
= line
.isReverseColor() ^ ch
.isReverse();
365 newCell
.setReverse(false);
367 if (ch
.getForeColorRGB() < 0) {
368 newCell
.setBackColor(ch
.getForeColor());
369 newCell
.setBackColorRGB(-1);
371 newCell
.setBackColorRGB(ch
.getForeColorRGB());
373 if (ch
.getBackColorRGB() < 0) {
374 newCell
.setForeColor(ch
.getBackColor());
375 newCell
.setForeColorRGB(-1);
377 newCell
.setForeColorRGB(ch
.getBackColorRGB());
380 if (line
.isDoubleWidth()) {
381 putDoubleWidthCharXY(line
, (i
* 2) + 1, row
, newCell
);
383 putCharXY(i
+ 1, row
, newCell
);
387 if (row
== getHeight() - 1) {
388 // Don't overwrite the box edge
392 CellAttributes background
= new CellAttributes();
393 // Fill in the blank lines on bottom
394 for (int i
= 0; i
< visibleHeight
; i
++) {
395 hLineXY(1, i
+ row
, getWidth() - 2, ' ', background
);
398 } // synchronized (emulator)
403 * Handle window close.
406 public void onClose() {
409 terminateShellChildProcess();
416 * Handle window/screen resize events.
418 * @param resize resize event
421 public void onResize(final TResizeEvent resize
) {
423 // Synchronize against the emulator so we don't stomp on its reader
425 synchronized (emulator
) {
427 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
428 // Resize the scroll bars
432 // Get out of scrollback
436 emulator
.setWidth(getWidth() - 2);
437 emulator
.setHeight(getHeight() - 2);
439 emulator
.writeRemote("\033[8;" + (getHeight() - 2) + ";" +
440 (getWidth() - 2) + "t");
445 } // synchronized (emulator)
449 * Resize scrollbars for a new width/height.
452 public void reflowData() {
454 // Synchronize against the emulator so we don't stomp on its reader
456 synchronized (emulator
) {
458 // Pull cursor information
461 // Vertical scrollbar
462 setTopValue(getHeight() - 2
463 - (emulator
.getScrollbackBuffer().size()
464 + emulator
.getDisplayBuffer().size()));
465 setVerticalBigChange(getHeight() - 2);
467 } // synchronized (emulator)
473 * @param keypress keystroke event
476 public void onKeypress(final TKeypressEvent keypress
) {
478 // Scrollback up/down
479 if (keypress
.equals(kbShiftPgUp
)
480 || keypress
.equals(kbCtrlPgUp
)
481 || keypress
.equals(kbAltPgUp
)
483 bigVerticalDecrement();
486 if (keypress
.equals(kbShiftPgDn
)
487 || keypress
.equals(kbCtrlPgDn
)
488 || keypress
.equals(kbAltPgDn
)
490 bigVerticalIncrement();
494 // Synchronize against the emulator so we don't stomp on its reader
496 synchronized (emulator
) {
497 if (emulator
.isReading()) {
498 // Get out of scrollback
500 emulator
.keypress(keypress
.getKey());
502 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
503 // this is kBEnter then also send kbCtrlJ.
504 if (System
.getProperty("os.name").startsWith("Windows")) {
505 if (keypress
.equals(kbEnter
)) {
506 emulator
.keypress(kbCtrlJ
);
515 // Process is closed, honor "normal" TUI keystrokes
516 super.onKeypress(keypress
);
520 * Handle mouse press events.
522 * @param mouse mouse button press event
525 public void onMouseDown(final TMouseEvent mouse
) {
526 if (inWindowMove
|| inWindowResize
) {
527 // TWindow needs to deal with this.
528 super.onMouseDown(mouse
);
532 // If the emulator is tracking mouse buttons, it needs to see wheel
534 if (emulator
.getMouseProtocol() == ECMA48
.MouseProtocol
.OFF
) {
535 if (mouse
.isMouseWheelUp()) {
539 if (mouse
.isMouseWheelDown()) {
544 if (mouseOnEmulator(mouse
)) {
545 synchronized (emulator
) {
546 mouse
.setX(mouse
.getX() - 1);
547 mouse
.setY(mouse
.getY() - 1);
548 emulator
.mouse(mouse
);
554 // Emulator didn't consume it, pass it on
555 super.onMouseDown(mouse
);
559 * Handle mouse release events.
561 * @param mouse mouse button release event
564 public void onMouseUp(final TMouseEvent mouse
) {
565 if (inWindowMove
|| inWindowResize
) {
566 // TWindow needs to deal with this.
567 super.onMouseUp(mouse
);
571 if (mouseOnEmulator(mouse
)) {
572 synchronized (emulator
) {
573 mouse
.setX(mouse
.getX() - 1);
574 mouse
.setY(mouse
.getY() - 1);
575 emulator
.mouse(mouse
);
581 // Emulator didn't consume it, pass it on
582 super.onMouseUp(mouse
);
586 * Handle mouse motion events.
588 * @param mouse mouse motion event
591 public void onMouseMotion(final TMouseEvent mouse
) {
592 if (inWindowMove
|| inWindowResize
) {
593 // TWindow needs to deal with this.
594 super.onMouseMotion(mouse
);
598 if (mouseOnEmulator(mouse
)) {
599 synchronized (emulator
) {
600 mouse
.setX(mouse
.getX() - 1);
601 mouse
.setY(mouse
.getY() - 1);
602 emulator
.mouse(mouse
);
608 // Emulator didn't consume it, pass it on
609 super.onMouseMotion(mouse
);
612 // ------------------------------------------------------------------------
613 // TTerminalWindow --------------------------------------------------------
614 // ------------------------------------------------------------------------
617 * Claim the keystrokes the emulator will need.
619 private void addShortcutKeys() {
620 addShortcutKeypress(kbCtrlA
);
621 addShortcutKeypress(kbCtrlB
);
622 addShortcutKeypress(kbCtrlC
);
623 addShortcutKeypress(kbCtrlD
);
624 addShortcutKeypress(kbCtrlE
);
625 addShortcutKeypress(kbCtrlF
);
626 addShortcutKeypress(kbCtrlG
);
627 addShortcutKeypress(kbCtrlH
);
628 addShortcutKeypress(kbCtrlU
);
629 addShortcutKeypress(kbCtrlJ
);
630 addShortcutKeypress(kbCtrlK
);
631 addShortcutKeypress(kbCtrlL
);
632 addShortcutKeypress(kbCtrlM
);
633 addShortcutKeypress(kbCtrlN
);
634 addShortcutKeypress(kbCtrlO
);
635 addShortcutKeypress(kbCtrlP
);
636 addShortcutKeypress(kbCtrlQ
);
637 addShortcutKeypress(kbCtrlR
);
638 addShortcutKeypress(kbCtrlS
);
639 addShortcutKeypress(kbCtrlT
);
640 addShortcutKeypress(kbCtrlU
);
641 addShortcutKeypress(kbCtrlV
);
642 addShortcutKeypress(kbCtrlW
);
643 addShortcutKeypress(kbCtrlX
);
644 addShortcutKeypress(kbCtrlY
);
645 addShortcutKeypress(kbCtrlZ
);
646 addShortcutKeypress(kbF1
);
647 addShortcutKeypress(kbF2
);
648 addShortcutKeypress(kbF3
);
649 addShortcutKeypress(kbF4
);
650 addShortcutKeypress(kbF5
);
651 addShortcutKeypress(kbF6
);
652 addShortcutKeypress(kbF7
);
653 addShortcutKeypress(kbF8
);
654 addShortcutKeypress(kbF9
);
655 addShortcutKeypress(kbF10
);
656 addShortcutKeypress(kbF11
);
657 addShortcutKeypress(kbF12
);
658 addShortcutKeypress(kbAltA
);
659 addShortcutKeypress(kbAltB
);
660 addShortcutKeypress(kbAltC
);
661 addShortcutKeypress(kbAltD
);
662 addShortcutKeypress(kbAltE
);
663 addShortcutKeypress(kbAltF
);
664 addShortcutKeypress(kbAltG
);
665 addShortcutKeypress(kbAltH
);
666 addShortcutKeypress(kbAltU
);
667 addShortcutKeypress(kbAltJ
);
668 addShortcutKeypress(kbAltK
);
669 addShortcutKeypress(kbAltL
);
670 addShortcutKeypress(kbAltM
);
671 addShortcutKeypress(kbAltN
);
672 addShortcutKeypress(kbAltO
);
673 addShortcutKeypress(kbAltP
);
674 addShortcutKeypress(kbAltQ
);
675 addShortcutKeypress(kbAltR
);
676 addShortcutKeypress(kbAltS
);
677 addShortcutKeypress(kbAltT
);
678 addShortcutKeypress(kbAltU
);
679 addShortcutKeypress(kbAltV
);
680 addShortcutKeypress(kbAltW
);
681 addShortcutKeypress(kbAltX
);
682 addShortcutKeypress(kbAltY
);
683 addShortcutKeypress(kbAltZ
);
687 * Convert a string array to a whitespace-separated string.
689 * @param array the string array
690 * @return a single string
692 private String
stringArrayToString(final String
[] array
) {
693 StringBuilder sb
= new StringBuilder(array
[0].length());
694 for (int i
= 0; i
< array
.length
; i
++) {
696 if (i
< array
.length
- 1) {
700 return sb
.toString();
706 * @param command the command line to execute
708 private void spawnShell(final String
[] command
) {
711 System.err.printf("spawnShell(): '%s'\n",
712 stringArrayToString(command));
715 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
719 ECMA48
.DeviceType deviceType
= ECMA48
.DeviceType
.XTERM
;
722 ProcessBuilder pb
= new ProcessBuilder(command
);
723 Map
<String
, String
> env
= pb
.environment();
724 env
.put("TERM", ECMA48
.deviceTypeTerm(deviceType
));
725 env
.put("LANG", ECMA48
.deviceTypeLang(deviceType
, "en"));
726 env
.put("COLUMNS", "80");
727 env
.put("LINES", "24");
728 pb
.redirectErrorStream(true);
730 emulator
= new ECMA48(deviceType
, shell
.getInputStream(),
731 shell
.getOutputStream(), this);
732 } catch (IOException e
) {
733 messageBox(i18n
.getString("errorLaunchingShellTitle"),
734 MessageFormat
.format(i18n
.getString("errorLaunchingShellText"),
738 // Setup the scroll bars
739 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
742 // Claim the keystrokes the emulator will need.
746 newStatusBar(i18n
.getString("statusBarRunning"));
748 // Pass the correct text cell width/height to the emulator
749 emulator
.setTextWidth(getScreen().getTextWidth());
750 emulator
.setTextHeight(getScreen().getTextHeight());
754 * Terminate the child of the 'script' process used on POSIX. This may
757 private void terminateShellChildProcess() {
759 if (shell
.getClass().getName().equals("java.lang.UNIXProcess")) {
760 /* get the PID on unix/linux systems */
762 Field field
= shell
.getClass().getDeclaredField("pid");
763 field
.setAccessible(true);
764 pid
= field
.getInt(shell
);
765 } catch (Throwable e
) {
766 // SQUASH, this didn't work. Just bail out quietly.
771 // shell.destroy() works successfully at killing this side of
772 // 'script'. But we need to make sure the other side (child
773 // process) is also killed.
774 String
[] cmdKillIt
= {
775 "pkill", "-P", Integer
.toString(pid
)
778 Runtime
.getRuntime().exec(cmdKillIt
);
779 } catch (Throwable e
) {
780 // SQUASH, this didn't work. Just bail out quietly.
787 * Called by emulator when fresh data has come in.
789 public void displayChanged() {
790 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
794 * Function to call to obtain the display width.
796 * @return the number of columns in the display
798 public int getDisplayWidth() {
800 return getWidth() - 2;
806 * Function to call to obtain the display height.
808 * @return the number of rows in the display
810 public int getDisplayHeight() {
812 return getHeight() - 2;
818 * Hook for subclasses to be notified of the shell termination.
820 public void onShellExit() {
824 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
828 * Copy out variables from the emulator that TTerminal has to expose on
831 private void readEmulatorState() {
832 // Synchronize against the emulator so we don't stomp on its reader
834 synchronized (emulator
) {
835 setHiddenMouse(emulator
.hasHiddenMousePointer());
837 setCursorX(emulator
.getCursorX() + 1);
838 setCursorY(emulator
.getCursorY() + 1
839 + (getHeight() - 2 - emulator
.getHeight())
840 - getVerticalValue());
841 setCursorVisible(emulator
.isCursorVisible());
842 if (getCursorX() > getWidth() - 2) {
843 setCursorVisible(false);
845 if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
846 setCursorVisible(false);
848 if (emulator
.getScreenTitle().length() > 0) {
849 // Only update the title if the shell is still alive
851 setTitle(emulator
.getScreenTitle());
855 // Check to see if the shell has died.
856 if (!emulator
.isReading() && (shell
!= null)) {
858 int rc
= shell
.exitValue();
859 // The emulator exited on its own, all is fine
860 setTitle(MessageFormat
.format(i18n
.
861 getString("windowTitleCompleted"), getTitle(), rc
));
864 clearShortcutKeypresses();
865 statusBar
.setText(MessageFormat
.format(i18n
.
866 getString("statusBarCompleted"), rc
));
868 } catch (IllegalThreadStateException e
) {
869 // The emulator thread has exited, but the shell Process
870 // hasn't figured that out yet. Do nothing, we will see
871 // this in a future tick.
873 } else if (emulator
.isReading() && (shell
!= null)) {
874 // The shell might be dead, let's check
876 int rc
= shell
.exitValue();
877 // If we got here, the shell died.
878 setTitle(MessageFormat
.format(i18n
.
879 getString("windowTitleCompleted"), getTitle(), rc
));
882 clearShortcutKeypresses();
883 statusBar
.setText(MessageFormat
.format(i18n
.
884 getString("statusBarCompleted"), rc
));
886 } catch (IllegalThreadStateException e
) {
887 // The shell is still running, do nothing.
891 } // synchronized (emulator)
895 * Check if a mouse press/release/motion event coordinate is over the
898 * @param mouse a mouse-based event
899 * @return whether or not the mouse is on the emulator
901 private boolean mouseOnEmulator(final TMouseEvent mouse
) {
903 synchronized (emulator
) {
904 if (!emulator
.isReading()) {
909 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
910 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
911 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
912 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
920 * Draw glyphs for a double-width or double-height VT100 cell to two
923 * @param line the line this VT100 cell is in
924 * @param x the X position to draw the left half to
925 * @param y the Y position to draw to
926 * @param cell the cell to draw
928 private void putDoubleWidthCharXY(final DisplayLine line
, final int x
,
929 final int y
, final Cell cell
) {
931 int textWidth
= getScreen().getTextWidth();
932 int textHeight
= getScreen().getTextHeight();
933 boolean cursorBlinkVisible
= true;
935 if (getScreen() instanceof SwingTerminal
) {
936 SwingTerminal terminal
= (SwingTerminal
) getScreen();
937 cursorBlinkVisible
= terminal
.getCursorBlinkVisible();
938 } else if (getScreen() instanceof ECMA48Terminal
) {
939 ECMA48Terminal terminal
= (ECMA48Terminal
) getScreen();
941 if (!terminal
.hasSixel()) {
942 // The backend does not have sixel support, draw this as text
944 putCharXY(x
, y
, cell
);
945 putCharXY(x
+ 1, y
, ' ', cell
);
948 cursorBlinkVisible
= blinkState
;
950 // We don't know how to dray glyphs to this screen, draw them as
951 // text and bail out.
952 putCharXY(x
, y
, cell
);
953 putCharXY(x
+ 1, y
, ' ', cell
);
957 if ((textWidth
!= lastTextWidth
) || (textHeight
!= lastTextHeight
)) {
958 // Screen size has changed, reset the font.
959 setupFont(textHeight
);
960 lastTextWidth
= textWidth
;
961 lastTextHeight
= textHeight
;
963 assert (doubleFont
!= null);
966 if (line
.getDoubleHeight() == 1) {
967 // Double-height top half: don't draw the underline.
968 Cell newCell
= new Cell();
970 newCell
.setUnderline(false);
971 image
= doubleFont
.getImage(newCell
, textWidth
* 2, textHeight
* 2,
974 image
= doubleFont
.getImage(cell
, textWidth
* 2, textHeight
* 2,
978 // Now that we have the double-wide glyph drawn, copy the right
979 // pieces of it to the cells.
980 Cell left
= new Cell();
981 Cell right
= new Cell();
985 BufferedImage leftImage
= null;
986 BufferedImage rightImage
= null;
988 System.err.println("image " + image + " textWidth " + textWidth +
989 " textHeight " + textHeight);
992 switch (line
.getDoubleHeight()) {
994 // Top half double height
995 leftImage
= image
.getSubimage(0, 0, textWidth
, textHeight
);
996 rightImage
= image
.getSubimage(textWidth
, 0, textWidth
, textHeight
);
999 // Bottom half double height
1000 leftImage
= image
.getSubimage(0, textHeight
, textWidth
, textHeight
);
1001 rightImage
= image
.getSubimage(textWidth
, textHeight
,
1002 textWidth
, textHeight
);
1005 // Either single height double-width, or error fallback
1006 BufferedImage wideImage
= new BufferedImage(textWidth
* 2,
1007 textHeight
, BufferedImage
.TYPE_INT_ARGB
);
1008 Graphics2D grWide
= wideImage
.createGraphics();
1009 grWide
.drawImage(image
, 0, 0, wideImage
.getWidth(),
1010 wideImage
.getHeight(), null);
1012 leftImage
= wideImage
.getSubimage(0, 0, textWidth
, textHeight
);
1013 rightImage
= wideImage
.getSubimage(textWidth
, 0, textWidth
,
1017 left
.setImage(leftImage
);
1018 right
.setImage(rightImage
);
1019 // Since we have image data, ditch the character here. Otherwise, a
1020 // drawBoxShadow() over the terminal window will show the characters
1021 // which looks wrong.
1024 putCharXY(x
, y
, left
);
1025 putCharXY(x
+ 1, y
, right
);
1029 * Set up the double-width font.
1031 * @param fontSize the size of font to request for the single-width font.
1032 * The double-width font will be 2x this value.
1034 private void setupFont(final int fontSize
) {
1035 doubleFont
= GlyphMaker
.getDefault().size(fontSize
* 2);
1037 // Special case: the ECMA48 backend needs to have a timer to drive
1039 if (getScreen() instanceof jexer
.backend
.ECMA48Terminal
) {
1041 // Blink every 500 millis.
1043 getApplication().addTimer(millis
, true,
1046 blinkState
= !blinkState
;
1047 getApplication().doRepaint();