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;
146 // ------------------------------------------------------------------------
147 // Constructors -----------------------------------------------------------
148 // ------------------------------------------------------------------------
151 * Public constructor spawns a custom command line.
153 * @param application TApplication that manages this window
154 * @param x column relative to parent
155 * @param y row relative to parent
156 * @param commandLine the command line to execute
158 public TTerminalWindow(final TApplication application
, final int x
,
159 final int y
, final String commandLine
) {
161 this(application
, x
, y
, RESIZABLE
, commandLine
.split("\\s+"),
162 System
.getProperty("jexer.TTerminal.closeOnExit",
163 "false").equals("true"));
167 * Public constructor spawns a custom command line.
169 * @param application TApplication that manages this window
170 * @param x column relative to parent
171 * @param y row relative to parent
172 * @param commandLine the command line to execute
173 * @param closeOnExit if true, close the window when the command exits
175 public TTerminalWindow(final TApplication application
, final int x
,
176 final int y
, final String commandLine
, final boolean closeOnExit
) {
178 this(application
, x
, y
, RESIZABLE
, commandLine
.split("\\s+"),
183 * Public constructor spawns a custom command line.
185 * @param application TApplication that manages this window
186 * @param x column relative to parent
187 * @param y row relative to parent
188 * @param flags mask of CENTERED, MODAL, or RESIZABLE
189 * @param command the command line to execute
191 public TTerminalWindow(final TApplication application
, final int x
,
192 final int y
, final int flags
, final String
[] command
) {
194 this(application
, x
, y
, flags
, command
,
195 System
.getProperty("jexer.TTerminal.closeOnExit",
196 "false").equals("true"));
200 * Public constructor spawns a custom command line.
202 * @param application TApplication that manages this window
203 * @param x column relative to parent
204 * @param y row relative to parent
205 * @param flags mask of CENTERED, MODAL, or RESIZABLE
206 * @param command the command line to execute
207 * @param closeOnExit if true, close the window when the command exits
209 public TTerminalWindow(final TApplication application
, final int x
,
210 final int y
, final int flags
, final String
[] command
,
211 final boolean closeOnExit
) {
213 super(application
, i18n
.getString("windowTitle"), x
, y
,
214 80 + 2, 24 + 2, flags
);
216 this.closeOnExit
= closeOnExit
;
218 String
[] fullCommand
;
220 // Spawn a shell and pass its I/O to the other constructor.
221 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
222 && (System
.getProperty("jexer.TTerminal.ptypipe").
226 fullCommand
= new String
[command
.length
+ 1];
227 fullCommand
[0] = "ptypipe";
228 System
.arraycopy(command
, 0, fullCommand
, 1, command
.length
);
229 } else if (System
.getProperty("os.name").startsWith("Windows")) {
230 fullCommand
= new String
[3];
231 fullCommand
[0] = "cmd";
232 fullCommand
[1] = "/c";
233 fullCommand
[2] = stringArrayToString(command
);
234 } else if (System
.getProperty("os.name").startsWith("Mac")) {
235 fullCommand
= new String
[6];
236 fullCommand
[0] = "script";
237 fullCommand
[1] = "-q";
238 fullCommand
[2] = "-F";
239 fullCommand
[3] = "/dev/null";
240 fullCommand
[4] = "-c";
241 fullCommand
[5] = stringArrayToString(command
);
243 // Default: behave like Linux
244 fullCommand
= new String
[5];
245 fullCommand
[0] = "script";
246 fullCommand
[1] = "-fqe";
247 fullCommand
[2] = "/dev/null";
248 fullCommand
[3] = "-c";
249 fullCommand
[4] = stringArrayToString(command
);
251 spawnShell(fullCommand
);
255 * Public constructor spawns a shell.
257 * @param application TApplication that manages this window
258 * @param x column relative to parent
259 * @param y row relative to parent
260 * @param flags mask of CENTERED, MODAL, or RESIZABLE
262 public TTerminalWindow(final TApplication application
, final int x
,
263 final int y
, final int flags
) {
265 this(application
, x
, y
, flags
,
266 System
.getProperty("jexer.TTerminal.closeOnExit",
267 "false").equals("true"));
272 * Public constructor spawns a shell.
274 * @param application TApplication that manages this window
275 * @param x column relative to parent
276 * @param y row relative to parent
277 * @param flags mask of CENTERED, MODAL, or RESIZABLE
278 * @param closeOnExit if true, close the window when the shell exits
280 public TTerminalWindow(final TApplication application
, final int x
,
281 final int y
, final int flags
, final boolean closeOnExit
) {
283 super(application
, i18n
.getString("windowTitle"), x
, y
,
284 80 + 2, 24 + 2, flags
);
286 this.closeOnExit
= closeOnExit
;
288 String cmdShellWindows
= "cmd.exe";
290 // You cannot run a login shell in a bare Process interactively, due
291 // to libc's behavior of buffering when stdin/stdout aren't a tty.
292 // Use 'script' instead to run a shell in a pty. And because BSD and
293 // GNU differ on the '-f' vs '-F' flags, we need two different
295 String cmdShellGNU
= "script -fqe /dev/null";
296 String cmdShellBSD
= "script -q -F /dev/null";
298 // ptypipe is another solution that permits dynamic window resizing.
299 String cmdShellPtypipe
= "ptypipe /bin/bash --login";
301 // Spawn a shell and pass its I/O to the other constructor.
302 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
303 && (System
.getProperty("jexer.TTerminal.ptypipe").
307 spawnShell(cmdShellPtypipe
.split("\\s+"));
308 } else if (System
.getProperty("os.name").startsWith("Windows")) {
309 spawnShell(cmdShellWindows
.split("\\s+"));
310 } else if (System
.getProperty("os.name").startsWith("Mac")) {
311 spawnShell(cmdShellBSD
.split("\\s+"));
312 } else if (System
.getProperty("os.name").startsWith("Linux")) {
313 spawnShell(cmdShellGNU
.split("\\s+"));
315 // When all else fails, assume GNU.
316 spawnShell(cmdShellGNU
.split("\\s+"));
320 // ------------------------------------------------------------------------
321 // TScrollableWindow ------------------------------------------------------
322 // ------------------------------------------------------------------------
325 * Draw the display buffer.
330 int width
= getDisplayWidth();
331 boolean syncEmulator
= false;
332 if ((System
.currentTimeMillis() - lastUpdateTime
> 125)
335 // Too much time has passed, draw it all.
337 } else if (emulator
.isReading() && (dirty
== false)) {
338 // Wait until the emulator has brought more data in.
339 syncEmulator
= false;
340 } else if (!emulator
.isReading() && (dirty
== true)) {
341 // The emulator won't receive more data, update the display.
345 if ((syncEmulator
== true)
346 || (scrollback
== null)
349 // We want to minimize the amount of time we have the emulator
350 // locked. Grab a copy of its display.
351 synchronized (emulator
) {
352 // Update the scroll bars
355 if ((scrollback
== null) || emulator
.isReading()) {
356 scrollback
= copyBuffer(emulator
.getScrollbackBuffer());
357 display
= copyBuffer(emulator
.getDisplayBuffer());
359 width
= emulator
.getWidth();
364 // Draw the box using my superclass
367 // Put together the visible rows
368 int visibleHeight
= getHeight() - 2;
369 int visibleBottom
= scrollback
.size() + display
.size()
370 + getVerticalValue();
371 assert (visibleBottom
>= 0);
373 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
374 int visibleTop
= visibleBottom
- visibleHeight
;
375 if (visibleTop
< 0) {
376 for (int i
= visibleTop
; i
< 0; i
++) {
377 preceedingBlankLines
.add(emulator
.getBlankDisplayLine());
381 assert (visibleTop
>= 0);
383 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
384 displayLines
.addAll(scrollback
);
385 displayLines
.addAll(display
);
387 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
388 visibleLines
.addAll(preceedingBlankLines
);
389 visibleLines
.addAll(displayLines
.subList(visibleTop
,
392 visibleHeight
-= visibleLines
.size();
393 assert (visibleHeight
>= 0);
395 // Now draw the emulator screen
397 for (DisplayLine line
: visibleLines
) {
398 int widthMax
= width
;
399 if (line
.isDoubleWidth()) {
402 if (widthMax
> getWidth() - 2) {
403 widthMax
= getWidth() - 2;
405 for (int i
= 0; i
< widthMax
; i
++) {
406 Cell ch
= line
.charAt(i
);
409 putCharXY(i
+ 1, row
, ch
);
413 Cell newCell
= new Cell();
415 boolean reverse
= line
.isReverseColor() ^ ch
.isReverse();
416 newCell
.setReverse(false);
418 if (ch
.getForeColorRGB() < 0) {
419 newCell
.setBackColor(ch
.getForeColor());
420 newCell
.setBackColorRGB(-1);
422 newCell
.setBackColorRGB(ch
.getForeColorRGB());
424 if (ch
.getBackColorRGB() < 0) {
425 newCell
.setForeColor(ch
.getBackColor());
426 newCell
.setForeColorRGB(-1);
428 newCell
.setForeColorRGB(ch
.getBackColorRGB());
431 if (line
.isDoubleWidth()) {
432 putDoubleWidthCharXY(line
, (i
* 2) + 1, row
, newCell
);
434 putCharXY(i
+ 1, row
, newCell
);
438 if (row
== getHeight() - 1) {
439 // Don't overwrite the box edge
443 CellAttributes background
= new CellAttributes();
444 // Fill in the blank lines on bottom
445 for (int i
= 0; i
< visibleHeight
; i
++) {
446 hLineXY(1, i
+ row
, getWidth() - 2, ' ', background
);
452 * Handle window close.
455 public void onClose() {
458 terminateShellChildProcess();
465 * Handle window/screen resize events.
467 * @param resize resize event
470 public void onResize(final TResizeEvent resize
) {
472 // Synchronize against the emulator so we don't stomp on its reader
474 synchronized (emulator
) {
476 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
477 // Resize the scroll bars
481 // Get out of scrollback
485 emulator
.setWidth(getWidth() - 2);
486 emulator
.setHeight(getHeight() - 2);
488 emulator
.writeRemote("\033[8;" + (getHeight() - 2) + ";" +
489 (getWidth() - 2) + "t");
494 } // synchronized (emulator)
498 * Resize scrollbars for a new width/height.
501 public void reflowData() {
503 // Synchronize against the emulator so we don't stomp on its reader
505 synchronized (emulator
) {
507 // Pull cursor information
510 // Vertical scrollbar
511 setTopValue(getHeight() - 2
512 - (emulator
.getScrollbackBuffer().size()
513 + emulator
.getDisplayBuffer().size()));
514 setVerticalBigChange(getHeight() - 2);
516 } // synchronized (emulator)
522 * @param keypress keystroke event
525 public void onKeypress(final TKeypressEvent keypress
) {
527 // Scrollback up/down
528 if (keypress
.equals(kbShiftPgUp
)
529 || keypress
.equals(kbCtrlPgUp
)
530 || keypress
.equals(kbAltPgUp
)
532 bigVerticalDecrement();
535 if (keypress
.equals(kbShiftPgDn
)
536 || keypress
.equals(kbCtrlPgDn
)
537 || keypress
.equals(kbAltPgDn
)
539 bigVerticalIncrement();
543 if (emulator
.isReading()) {
544 // Get out of scrollback
546 emulator
.addUserEvent(keypress
);
548 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
549 // this is kBEnter then also send kbCtrlJ.
550 if (System
.getProperty("os.name").startsWith("Windows")) {
551 if (keypress
.equals(kbEnter
)) {
552 emulator
.addUserEvent(new TKeypressEvent(kbCtrlJ
));
560 // Process is closed, honor "normal" TUI keystrokes
561 super.onKeypress(keypress
);
565 * Handle mouse press events.
567 * @param mouse mouse button press event
570 public void onMouseDown(final TMouseEvent mouse
) {
571 if (inWindowMove
|| inWindowResize
) {
572 // TWindow needs to deal with this.
573 super.onMouseDown(mouse
);
577 // If the emulator is tracking mouse buttons, it needs to see wheel
579 if (emulator
.getMouseProtocol() == ECMA48
.MouseProtocol
.OFF
) {
580 if (mouse
.isMouseWheelUp()) {
584 if (mouse
.isMouseWheelDown()) {
589 if (mouseOnEmulator(mouse
)) {
590 mouse
.setX(mouse
.getX() - 1);
591 mouse
.setY(mouse
.getY() - 1);
592 emulator
.addUserEvent(mouse
);
597 // Emulator didn't consume it, pass it on
598 super.onMouseDown(mouse
);
602 * Handle mouse release events.
604 * @param mouse mouse button release event
607 public void onMouseUp(final TMouseEvent mouse
) {
608 if (inWindowMove
|| inWindowResize
) {
609 // TWindow needs to deal with this.
610 super.onMouseUp(mouse
);
614 if (mouseOnEmulator(mouse
)) {
615 mouse
.setX(mouse
.getX() - 1);
616 mouse
.setY(mouse
.getY() - 1);
617 emulator
.addUserEvent(mouse
);
622 // Emulator didn't consume it, pass it on
623 super.onMouseUp(mouse
);
627 * Handle mouse motion events.
629 * @param mouse mouse motion event
632 public void onMouseMotion(final TMouseEvent mouse
) {
633 if (inWindowMove
|| inWindowResize
) {
634 // TWindow needs to deal with this.
635 super.onMouseMotion(mouse
);
639 if (mouseOnEmulator(mouse
)) {
640 mouse
.setX(mouse
.getX() - 1);
641 mouse
.setY(mouse
.getY() - 1);
642 emulator
.addUserEvent(mouse
);
647 // Emulator didn't consume it, pass it on
648 super.onMouseMotion(mouse
);
651 // ------------------------------------------------------------------------
652 // TTerminalWindow --------------------------------------------------------
653 // ------------------------------------------------------------------------
656 * Claim the keystrokes the emulator will need.
658 private void addShortcutKeys() {
659 addShortcutKeypress(kbCtrlA
);
660 addShortcutKeypress(kbCtrlB
);
661 addShortcutKeypress(kbCtrlC
);
662 addShortcutKeypress(kbCtrlD
);
663 addShortcutKeypress(kbCtrlE
);
664 addShortcutKeypress(kbCtrlF
);
665 addShortcutKeypress(kbCtrlG
);
666 addShortcutKeypress(kbCtrlH
);
667 addShortcutKeypress(kbCtrlU
);
668 addShortcutKeypress(kbCtrlJ
);
669 addShortcutKeypress(kbCtrlK
);
670 addShortcutKeypress(kbCtrlL
);
671 addShortcutKeypress(kbCtrlM
);
672 addShortcutKeypress(kbCtrlN
);
673 addShortcutKeypress(kbCtrlO
);
674 addShortcutKeypress(kbCtrlP
);
675 addShortcutKeypress(kbCtrlQ
);
676 addShortcutKeypress(kbCtrlR
);
677 addShortcutKeypress(kbCtrlS
);
678 addShortcutKeypress(kbCtrlT
);
679 addShortcutKeypress(kbCtrlU
);
680 addShortcutKeypress(kbCtrlV
);
681 addShortcutKeypress(kbCtrlW
);
682 addShortcutKeypress(kbCtrlX
);
683 addShortcutKeypress(kbCtrlY
);
684 addShortcutKeypress(kbCtrlZ
);
685 addShortcutKeypress(kbF1
);
686 addShortcutKeypress(kbF2
);
687 addShortcutKeypress(kbF3
);
688 addShortcutKeypress(kbF4
);
689 addShortcutKeypress(kbF5
);
690 addShortcutKeypress(kbF6
);
691 addShortcutKeypress(kbF7
);
692 addShortcutKeypress(kbF8
);
693 addShortcutKeypress(kbF9
);
694 addShortcutKeypress(kbF10
);
695 addShortcutKeypress(kbF11
);
696 addShortcutKeypress(kbF12
);
697 addShortcutKeypress(kbAltA
);
698 addShortcutKeypress(kbAltB
);
699 addShortcutKeypress(kbAltC
);
700 addShortcutKeypress(kbAltD
);
701 addShortcutKeypress(kbAltE
);
702 addShortcutKeypress(kbAltF
);
703 addShortcutKeypress(kbAltG
);
704 addShortcutKeypress(kbAltH
);
705 addShortcutKeypress(kbAltU
);
706 addShortcutKeypress(kbAltJ
);
707 addShortcutKeypress(kbAltK
);
708 addShortcutKeypress(kbAltL
);
709 addShortcutKeypress(kbAltM
);
710 addShortcutKeypress(kbAltN
);
711 addShortcutKeypress(kbAltO
);
712 addShortcutKeypress(kbAltP
);
713 addShortcutKeypress(kbAltQ
);
714 addShortcutKeypress(kbAltR
);
715 addShortcutKeypress(kbAltS
);
716 addShortcutKeypress(kbAltT
);
717 addShortcutKeypress(kbAltU
);
718 addShortcutKeypress(kbAltV
);
719 addShortcutKeypress(kbAltW
);
720 addShortcutKeypress(kbAltX
);
721 addShortcutKeypress(kbAltY
);
722 addShortcutKeypress(kbAltZ
);
726 * Convert a string array to a whitespace-separated string.
728 * @param array the string array
729 * @return a single string
731 private String
stringArrayToString(final String
[] array
) {
732 StringBuilder sb
= new StringBuilder(array
[0].length());
733 for (int i
= 0; i
< array
.length
; i
++) {
735 if (i
< array
.length
- 1) {
739 return sb
.toString();
745 * @param command the command line to execute
747 private void spawnShell(final String
[] command
) {
750 System.err.printf("spawnShell(): '%s'\n",
751 stringArrayToString(command));
754 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
758 ECMA48
.DeviceType deviceType
= ECMA48
.DeviceType
.XTERM
;
761 ProcessBuilder pb
= new ProcessBuilder(command
);
762 Map
<String
, String
> env
= pb
.environment();
763 env
.put("TERM", ECMA48
.deviceTypeTerm(deviceType
));
764 env
.put("LANG", ECMA48
.deviceTypeLang(deviceType
, "en"));
765 env
.put("COLUMNS", "80");
766 env
.put("LINES", "24");
767 pb
.redirectErrorStream(true);
769 emulator
= new ECMA48(deviceType
, shell
.getInputStream(),
770 shell
.getOutputStream(), this);
771 } catch (IOException e
) {
772 messageBox(i18n
.getString("errorLaunchingShellTitle"),
773 MessageFormat
.format(i18n
.getString("errorLaunchingShellText"),
777 // Setup the scroll bars
778 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
781 // Claim the keystrokes the emulator will need.
785 newStatusBar(i18n
.getString("statusBarRunning"));
787 // Pass the correct text cell width/height to the emulator
788 emulator
.setTextWidth(getScreen().getTextWidth());
789 emulator
.setTextHeight(getScreen().getTextHeight());
793 * Terminate the child of the 'script' process used on POSIX. This may
796 private void terminateShellChildProcess() {
798 if (shell
.getClass().getName().equals("java.lang.UNIXProcess")) {
799 /* get the PID on unix/linux systems */
801 Field field
= shell
.getClass().getDeclaredField("pid");
802 field
.setAccessible(true);
803 pid
= field
.getInt(shell
);
804 } catch (Throwable e
) {
805 // SQUASH, this didn't work. Just bail out quietly.
810 // shell.destroy() works successfully at killing this side of
811 // 'script'. But we need to make sure the other side (child
812 // process) is also killed.
813 String
[] cmdKillIt
= {
814 "pkill", "-P", Integer
.toString(pid
)
817 Runtime
.getRuntime().exec(cmdKillIt
);
818 } catch (Throwable e
) {
819 // SQUASH, this didn't work. Just bail out quietly.
826 * Hook for subclasses to be notified of the shell termination.
828 public void onShellExit() {
832 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
836 * Copy out variables from the emulator that TTerminal has to expose on
839 private void readEmulatorState() {
840 // Synchronize against the emulator so we don't stomp on its reader
842 synchronized (emulator
) {
843 setHiddenMouse(emulator
.hasHiddenMousePointer());
845 setCursorX(emulator
.getCursorX() + 1);
846 setCursorY(emulator
.getCursorY() + 1
847 + (getHeight() - 2 - emulator
.getHeight())
848 - getVerticalValue());
849 setCursorVisible(emulator
.isCursorVisible());
850 if (getCursorX() > getWidth() - 2) {
851 setCursorVisible(false);
853 if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
854 setCursorVisible(false);
856 if (emulator
.getScreenTitle().length() > 0) {
857 // Only update the title if the shell is still alive
859 setTitle(emulator
.getScreenTitle());
863 // Check to see if the shell has died.
864 if (!emulator
.isReading() && (shell
!= null)) {
866 int rc
= shell
.exitValue();
867 // The emulator exited on its own, all is fine
868 setTitle(MessageFormat
.format(i18n
.
869 getString("windowTitleCompleted"), getTitle(), rc
));
872 clearShortcutKeypresses();
873 statusBar
.setText(MessageFormat
.format(i18n
.
874 getString("statusBarCompleted"), rc
));
876 } catch (IllegalThreadStateException e
) {
877 // The emulator thread has exited, but the shell Process
878 // hasn't figured that out yet. Do nothing, we will see
879 // this in a future tick.
881 } else if (emulator
.isReading() && (shell
!= null)) {
882 // The shell might be dead, let's check
884 int rc
= shell
.exitValue();
885 // If we got here, the shell died.
886 setTitle(MessageFormat
.format(i18n
.
887 getString("windowTitleCompleted"), getTitle(), rc
));
890 clearShortcutKeypresses();
891 statusBar
.setText(MessageFormat
.format(i18n
.
892 getString("statusBarCompleted"), rc
));
894 } catch (IllegalThreadStateException e
) {
895 // The shell is still running, do nothing.
899 } // synchronized (emulator)
903 * Check if a mouse press/release/motion event coordinate is over the
906 * @param mouse a mouse-based event
907 * @return whether or not the mouse is on the emulator
909 private boolean mouseOnEmulator(final TMouseEvent mouse
) {
911 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 * Copy a display buffer.
928 * @param buffer the buffer to copy
929 * @return a deep copy of the buffer's data
931 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
932 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
933 for (DisplayLine line
: buffer
) {
934 result
.add(new DisplayLine(line
));
940 * Draw glyphs for a double-width or double-height VT100 cell to two
943 * @param line the line this VT100 cell is in
944 * @param x the X position to draw the left half to
945 * @param y the Y position to draw to
946 * @param cell the cell to draw
948 private void putDoubleWidthCharXY(final DisplayLine line
, final int x
,
949 final int y
, final Cell cell
) {
951 int textWidth
= getScreen().getTextWidth();
952 int textHeight
= getScreen().getTextHeight();
953 boolean cursorBlinkVisible
= true;
955 if (getScreen() instanceof SwingTerminal
) {
956 SwingTerminal terminal
= (SwingTerminal
) getScreen();
957 cursorBlinkVisible
= terminal
.getCursorBlinkVisible();
958 } else if (getScreen() instanceof ECMA48Terminal
) {
959 ECMA48Terminal terminal
= (ECMA48Terminal
) getScreen();
961 if (!terminal
.hasSixel()) {
962 // The backend does not have sixel support, draw this as text
964 putCharXY(x
, y
, cell
);
965 putCharXY(x
+ 1, y
, ' ', cell
);
968 cursorBlinkVisible
= blinkState
;
970 // We don't know how to dray glyphs to this screen, draw them as
971 // text and bail out.
972 putCharXY(x
, y
, cell
);
973 putCharXY(x
+ 1, y
, ' ', cell
);
977 if ((textWidth
!= lastTextWidth
) || (textHeight
!= lastTextHeight
)) {
978 // Screen size has changed, reset the font.
979 setupFont(textHeight
);
980 lastTextWidth
= textWidth
;
981 lastTextHeight
= textHeight
;
983 assert (doubleFont
!= null);
986 if (line
.getDoubleHeight() == 1) {
987 // Double-height top half: don't draw the underline.
988 Cell newCell
= new Cell();
990 newCell
.setUnderline(false);
991 image
= doubleFont
.getImage(newCell
, textWidth
* 2, textHeight
* 2,
994 image
= doubleFont
.getImage(cell
, textWidth
* 2, textHeight
* 2,
998 // Now that we have the double-wide glyph drawn, copy the right
999 // pieces of it to the cells.
1000 Cell left
= new Cell();
1001 Cell right
= new Cell();
1005 BufferedImage leftImage
= null;
1006 BufferedImage rightImage
= null;
1008 System.err.println("image " + image + " textWidth " + textWidth +
1009 " textHeight " + textHeight);
1012 switch (line
.getDoubleHeight()) {
1014 // Top half double height
1015 leftImage
= image
.getSubimage(0, 0, textWidth
, textHeight
);
1016 rightImage
= image
.getSubimage(textWidth
, 0, textWidth
, textHeight
);
1019 // Bottom half double height
1020 leftImage
= image
.getSubimage(0, textHeight
, textWidth
, textHeight
);
1021 rightImage
= image
.getSubimage(textWidth
, textHeight
,
1022 textWidth
, textHeight
);
1025 // Either single height double-width, or error fallback
1026 BufferedImage wideImage
= new BufferedImage(textWidth
* 2,
1027 textHeight
, BufferedImage
.TYPE_INT_ARGB
);
1028 Graphics2D grWide
= wideImage
.createGraphics();
1029 grWide
.drawImage(image
, 0, 0, wideImage
.getWidth(),
1030 wideImage
.getHeight(), null);
1032 leftImage
= wideImage
.getSubimage(0, 0, textWidth
, textHeight
);
1033 rightImage
= wideImage
.getSubimage(textWidth
, 0, textWidth
,
1037 left
.setImage(leftImage
);
1038 right
.setImage(rightImage
);
1039 // Since we have image data, ditch the character here. Otherwise, a
1040 // drawBoxShadow() over the terminal window will show the characters
1041 // which looks wrong.
1044 putCharXY(x
, y
, left
);
1045 putCharXY(x
+ 1, y
, right
);
1049 * Set up the double-width font.
1051 * @param fontSize the size of font to request for the single-width font.
1052 * The double-width font will be 2x this value.
1054 private void setupFont(final int fontSize
) {
1055 doubleFont
= GlyphMaker
.getInstance(fontSize
* 2);
1057 // Special case: the ECMA48 backend needs to have a timer to drive
1059 if (getScreen() instanceof jexer
.backend
.ECMA48Terminal
) {
1061 // Blink every 500 millis.
1063 getApplication().addTimer(millis
, true,
1066 blinkState
= !blinkState
;
1067 getApplication().doRepaint();
1076 // ------------------------------------------------------------------------
1077 // DisplayListener --------------------------------------------------------
1078 // ------------------------------------------------------------------------
1081 * Called by emulator when fresh data has come in.
1083 public void displayChanged() {
1085 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
1089 * Function to call to obtain the display width.
1091 * @return the number of columns in the display
1093 public int getDisplayWidth() {
1095 return getWidth() - 2;
1101 * Function to call to obtain the display height.
1103 * @return the number of rows in the display
1105 public int getDisplayHeight() {
1107 return getHeight() - 2;