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 * TTerminalWidget exposes a ECMA-48 / ANSI X3.64 style terminal in a widget.
65 public class TTerminalWidget
extends TScrollableWidget
66 implements DisplayListener
{
71 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TTerminalWidget
.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;
97 private GlyphMaker doubleFont
;
100 * Last text width value.
102 private int lastTextWidth
= -1;
105 * Last text height value.
107 private int lastTextHeight
= -1;
110 * The blink state, used only by ECMA48 backend and when double-width
111 * chars must be drawn.
113 private boolean blinkState
= true;
116 * Timer flag, used only by ECMA48 backend and when double-width chars
119 private boolean haveTimer
= false;
122 * The last seen scrollback lines.
124 private List
<DisplayLine
> scrollback
;
127 * The last seen display lines.
129 private List
<DisplayLine
> display
;
132 * If true, the display has changed and needs updating.
134 private volatile boolean dirty
= true;
137 * Time that the display was last updated.
139 private long lastUpdateTime
= 0;
142 * If true, hide the mouse after typing a keystroke.
144 private boolean hideMouseWhenTyping
= true;
147 * If true, the mouse should not be displayed because a keystroke was
150 private boolean typingHidMouse
= false;
153 * The return value from the emulator.
155 private int exitValue
= -1;
158 * Title to expose to a window.
160 private String title
= "";
163 * Action to perform when the terminal exits.
165 private TAction closeAction
= null;
167 // ------------------------------------------------------------------------
168 // Constructors -----------------------------------------------------------
169 // ------------------------------------------------------------------------
172 * Public constructor spawns a custom command line.
174 * @param parent parent widget
175 * @param x column relative to parent
176 * @param y row relative to parent
177 * @param commandLine the command line to execute
179 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
180 final String commandLine
) {
182 this(parent
, x
, y
, commandLine
.split("\\s+"));
186 * Public constructor spawns a custom command line.
188 * @param parent parent widget
189 * @param x column relative to parent
190 * @param y row relative to parent
191 * @param command the command line to execute
193 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
194 final String
[] command
) {
196 this(parent
, x
, y
, command
, null);
200 * Public constructor spawns a custom command line.
202 * @param parent parent widget
203 * @param x column relative to parent
204 * @param y row relative to parent
205 * @param command the command line to execute
206 * @param closeAction action to perform when the shell sxits
208 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
209 final String
[] command
, final TAction closeAction
) {
211 this(parent
, x
, y
, 80, 24, command
, closeAction
);
215 * Public constructor spawns a custom command line.
217 * @param parent parent widget
218 * @param x column relative to parent
219 * @param y row relative to parent
220 * @param width width of widget
221 * @param height height of widget
222 * @param command the command line to execute
223 * @param closeAction action to perform when the shell sxits
225 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
226 final int width
, final int height
, final String
[] command
,
227 final TAction closeAction
) {
229 super(parent
, x
, y
, width
, height
);
231 this.closeAction
= closeAction
;
233 String
[] fullCommand
;
235 // Spawn a shell and pass its I/O to the other constructor.
236 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
237 && (System
.getProperty("jexer.TTerminal.ptypipe").
241 fullCommand
= new String
[command
.length
+ 1];
242 fullCommand
[0] = "ptypipe";
243 System
.arraycopy(command
, 0, fullCommand
, 1, command
.length
);
244 } else if (System
.getProperty("os.name").startsWith("Windows")) {
245 fullCommand
= new String
[3];
246 fullCommand
[0] = "cmd";
247 fullCommand
[1] = "/c";
248 fullCommand
[2] = stringArrayToString(command
);
249 } else if (System
.getProperty("os.name").startsWith("Mac")) {
250 fullCommand
= new String
[6];
251 fullCommand
[0] = "script";
252 fullCommand
[1] = "-q";
253 fullCommand
[2] = "-F";
254 fullCommand
[3] = "/dev/null";
255 fullCommand
[4] = "-c";
256 fullCommand
[5] = stringArrayToString(command
);
258 // Default: behave like Linux
259 fullCommand
= new String
[5];
260 fullCommand
[0] = "script";
261 fullCommand
[1] = "-fqe";
262 fullCommand
[2] = "/dev/null";
263 fullCommand
[3] = "-c";
264 fullCommand
[4] = stringArrayToString(command
);
266 spawnShell(fullCommand
);
270 * Public constructor spawns a shell.
272 * @param parent parent widget
273 * @param x column relative to parent
274 * @param y row relative to parent
276 public TTerminalWidget(final TWidget parent
, final int x
, final int y
) {
277 this(parent
, x
, y
, (TAction
) null);
281 * Public constructor spawns a shell.
283 * @param parent parent widget
284 * @param x column relative to parent
285 * @param y row relative to parent
286 * @param closeAction action to perform when the shell sxits
288 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
289 final TAction closeAction
) {
291 this(parent
, x
, y
, 80, 24, closeAction
);
295 * Public constructor spawns a shell.
297 * @param parent parent widget
298 * @param x column relative to parent
299 * @param y row relative to parent
300 * @param width width of widget
301 * @param height height of widget
302 * @param closeAction action to perform when the shell sxits
304 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
305 final int width
, final int height
, final TAction closeAction
) {
307 super(parent
, x
, y
, width
, height
);
309 this.closeAction
= closeAction
;
311 if (System
.getProperty("jexer.TTerminal.shell") != null) {
312 String shell
= System
.getProperty("jexer.TTerminal.shell");
313 if (shell
.trim().startsWith("ptypipe")) {
316 spawnShell(shell
.split("\\s+"));
320 String cmdShellWindows
= "cmd.exe";
322 // You cannot run a login shell in a bare Process interactively, due
323 // to libc's behavior of buffering when stdin/stdout aren't a tty.
324 // Use 'script' instead to run a shell in a pty. And because BSD and
325 // GNU differ on the '-f' vs '-F' flags, we need two different
327 String cmdShellGNU
= "script -fqe /dev/null";
328 String cmdShellBSD
= "script -q -F /dev/null";
330 // ptypipe is another solution that permits dynamic window resizing.
331 String cmdShellPtypipe
= "ptypipe /bin/bash --login";
333 // Spawn a shell and pass its I/O to the other constructor.
334 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
335 && (System
.getProperty("jexer.TTerminal.ptypipe").
339 spawnShell(cmdShellPtypipe
.split("\\s+"));
340 } else if (System
.getProperty("os.name").startsWith("Windows")) {
341 spawnShell(cmdShellWindows
.split("\\s+"));
342 } else if (System
.getProperty("os.name").startsWith("Mac")) {
343 spawnShell(cmdShellBSD
.split("\\s+"));
344 } else if (System
.getProperty("os.name").startsWith("Linux")) {
345 spawnShell(cmdShellGNU
.split("\\s+"));
347 // When all else fails, assume GNU.
348 spawnShell(cmdShellGNU
.split("\\s+"));
352 // ------------------------------------------------------------------------
353 // TScrollableWidget ------------------------------------------------------
354 // ------------------------------------------------------------------------
357 * Draw the display buffer.
361 int width
= getDisplayWidth();
363 boolean syncEmulator
= false;
364 if ((System
.currentTimeMillis() - lastUpdateTime
>= 25)
367 // Too much time has passed, draw it all.
369 } else if (emulator
.isReading() && (dirty
== false)) {
370 // Wait until the emulator has brought more data in.
371 syncEmulator
= false;
372 } else if (!emulator
.isReading() && (dirty
== true)) {
373 // The emulator won't receive more data, update the display.
377 if ((syncEmulator
== true)
378 || (scrollback
== null)
381 // We want to minimize the amount of time we have the emulator
382 // locked. Grab a copy of its display.
383 synchronized (emulator
) {
384 // Update the scroll bars
388 // We lost the connection, onShellExit() called an action
389 // that ultimately removed this widget from the UI
390 // hierarchy, so no one cares if we update the display.
395 if ((scrollback
== null) || emulator
.isReading()) {
396 scrollback
= copyBuffer(emulator
.getScrollbackBuffer());
397 display
= copyBuffer(emulator
.getDisplayBuffer());
399 width
= emulator
.getWidth();
404 // Put together the visible rows
405 int visibleHeight
= getHeight();
406 int visibleBottom
= scrollback
.size() + display
.size()
407 + getVerticalValue();
408 assert (visibleBottom
>= 0);
410 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
411 int visibleTop
= visibleBottom
- visibleHeight
;
412 if (visibleTop
< 0) {
413 for (int i
= visibleTop
; i
< 0; i
++) {
414 preceedingBlankLines
.add(emulator
.getBlankDisplayLine());
418 assert (visibleTop
>= 0);
420 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
421 displayLines
.addAll(scrollback
);
422 displayLines
.addAll(display
);
424 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
425 visibleLines
.addAll(preceedingBlankLines
);
426 visibleLines
.addAll(displayLines
.subList(visibleTop
,
429 visibleHeight
-= visibleLines
.size();
430 assert (visibleHeight
>= 0);
432 // Now draw the emulator screen
434 for (DisplayLine line
: visibleLines
) {
435 int widthMax
= width
;
436 if (line
.isDoubleWidth()) {
439 if (widthMax
> getWidth()) {
440 widthMax
= getWidth();
442 for (int i
= 0; i
< widthMax
; i
++) {
443 Cell ch
= line
.charAt(i
);
446 putCharXY(i
, row
, ch
);
450 Cell newCell
= new Cell(ch
);
451 boolean reverse
= line
.isReverseColor() ^ ch
.isReverse();
452 newCell
.setReverse(false);
454 if (ch
.getForeColorRGB() < 0) {
455 newCell
.setBackColor(ch
.getForeColor());
456 newCell
.setBackColorRGB(-1);
458 newCell
.setBackColorRGB(ch
.getForeColorRGB());
460 if (ch
.getBackColorRGB() < 0) {
461 newCell
.setForeColor(ch
.getBackColor());
462 newCell
.setForeColorRGB(-1);
464 newCell
.setForeColorRGB(ch
.getBackColorRGB());
467 if (line
.isDoubleWidth()) {
468 putDoubleWidthCharXY(line
, (i
* 2), row
, newCell
);
470 putCharXY(i
, row
, newCell
);
474 if (row
== getHeight()) {
475 // Don't overwrite the box edge
479 CellAttributes background
= new CellAttributes();
480 // Fill in the blank lines on bottom
481 for (int i
= 0; i
< visibleHeight
; i
++) {
482 hLineXY(0, i
+ row
, getWidth(), ' ', background
);
488 * Handle widget close.
491 public void close() {
494 terminateShellChildProcess();
501 * Handle window/screen resize events.
503 * @param resize resize event
506 public void onResize(final TResizeEvent resize
) {
507 // Let TWidget set my size.
508 super.onResize(resize
);
510 // Synchronize against the emulator so we don't stomp on its reader
512 synchronized (emulator
) {
514 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
515 // Resize the scroll bars
519 // Get out of scrollback
523 emulator
.setWidth(getWidth());
524 emulator
.setHeight(getHeight());
526 emulator
.writeRemote("\033[8;" + getHeight() + ";" +
530 // Pass the correct text cell width/height to the emulator
531 if (getScreen() != null) {
532 emulator
.setTextWidth(getScreen().getTextWidth());
533 emulator
.setTextHeight(getScreen().getTextHeight());
538 } // synchronized (emulator)
542 * Resize scrollbars for a new width/height.
545 public void reflowData() {
547 // Synchronize against the emulator so we don't stomp on its reader
549 synchronized (emulator
) {
551 // Pull cursor information
554 // Vertical scrollbar
555 setTopValue(getHeight()
556 - (emulator
.getScrollbackBuffer().size()
557 + emulator
.getDisplayBuffer().size()));
558 setVerticalBigChange(getHeight());
560 } // synchronized (emulator)
566 * @param keypress keystroke event
569 public void onKeypress(final TKeypressEvent keypress
) {
570 if (hideMouseWhenTyping
) {
571 typingHidMouse
= true;
574 // Scrollback up/down
575 if (keypress
.equals(kbShiftPgUp
)
576 || keypress
.equals(kbCtrlPgUp
)
577 || keypress
.equals(kbAltPgUp
)
579 bigVerticalDecrement();
582 if (keypress
.equals(kbShiftPgDn
)
583 || keypress
.equals(kbCtrlPgDn
)
584 || keypress
.equals(kbAltPgDn
)
586 bigVerticalIncrement();
590 if (emulator
.isReading()) {
591 // Get out of scrollback
593 emulator
.addUserEvent(keypress
);
595 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
596 // this is kBEnter then also send kbCtrlJ.
597 if (keypress
.equals(kbEnter
)) {
598 if (System
.getProperty("os.name").startsWith("Windows")
599 && (System
.getProperty("jexer.TTerminal.cmdHack",
600 "true").equals("true"))
602 emulator
.addUserEvent(new TKeypressEvent(kbCtrlJ
));
610 // Process is closed, honor "normal" TUI keystrokes
611 super.onKeypress(keypress
);
615 * Handle mouse press events.
617 * @param mouse mouse button press event
620 public void onMouseDown(final TMouseEvent mouse
) {
621 if (hideMouseWhenTyping
) {
622 typingHidMouse
= false;
625 // If the emulator is tracking mouse buttons, it needs to see wheel
627 if (emulator
.getMouseProtocol() == ECMA48
.MouseProtocol
.OFF
) {
628 if (mouse
.isMouseWheelUp()) {
632 if (mouse
.isMouseWheelDown()) {
637 if (mouseOnEmulator(mouse
)) {
638 emulator
.addUserEvent(mouse
);
643 // Emulator didn't consume it, pass it on
644 super.onMouseDown(mouse
);
648 * Handle mouse release events.
650 * @param mouse mouse button release event
653 public void onMouseUp(final TMouseEvent mouse
) {
654 if (hideMouseWhenTyping
) {
655 typingHidMouse
= false;
658 if (mouseOnEmulator(mouse
)) {
659 emulator
.addUserEvent(mouse
);
664 // Emulator didn't consume it, pass it on
665 super.onMouseUp(mouse
);
669 * Handle mouse motion events.
671 * @param mouse mouse motion event
674 public void onMouseMotion(final TMouseEvent mouse
) {
675 if (hideMouseWhenTyping
) {
676 typingHidMouse
= false;
679 if (mouseOnEmulator(mouse
)) {
680 emulator
.addUserEvent(mouse
);
685 // Emulator didn't consume it, pass it on
686 super.onMouseMotion(mouse
);
689 // ------------------------------------------------------------------------
690 // TTerminalWidget --------------------------------------------------------
691 // ------------------------------------------------------------------------
694 * Get the desired window title.
698 public String
getTitle() {
703 * Returns true if this widget does not want the application-wide mouse
704 * cursor drawn over it.
706 * @return true if this widget does not want the application-wide mouse
707 * cursor drawn over it
709 public boolean hasHiddenMouse() {
710 return (emulator
.hasHiddenMousePointer() || typingHidMouse
);
714 * See if the terminal is still running.
716 * @return if true, we are still connected to / reading from the remote
719 public boolean isReading() {
720 return emulator
.isReading();
724 * Convert a string array to a whitespace-separated string.
726 * @param array the string array
727 * @return a single string
729 private String
stringArrayToString(final String
[] array
) {
730 StringBuilder sb
= new StringBuilder(array
[0].length());
731 for (int i
= 0; i
< array
.length
; i
++) {
733 if (i
< array
.length
- 1) {
737 return sb
.toString();
743 * @param command the command line to execute
745 private void spawnShell(final String
[] command
) {
748 System.err.printf("spawnShell(): '%s'\n",
749 stringArrayToString(command));
752 // We will have vScroller for its data fields and mouse event
753 // handling, but do not want to draw it.
754 vScroller
= new TVScroller(null, getWidth(), 0, getHeight());
755 vScroller
.setVisible(false);
758 title
= i18n
.getString("windowTitle");
761 ECMA48
.DeviceType deviceType
= ECMA48
.DeviceType
.XTERM
;
764 ProcessBuilder pb
= new ProcessBuilder(command
);
765 Map
<String
, String
> env
= pb
.environment();
766 env
.put("TERM", ECMA48
.deviceTypeTerm(deviceType
));
767 env
.put("LANG", ECMA48
.deviceTypeLang(deviceType
, "en"));
768 env
.put("COLUMNS", "80");
769 env
.put("LINES", "24");
770 pb
.redirectErrorStream(true);
772 emulator
= new ECMA48(deviceType
, shell
.getInputStream(),
773 shell
.getOutputStream(), this);
774 } catch (IOException e
) {
775 messageBox(i18n
.getString("errorLaunchingShellTitle"),
776 MessageFormat
.format(i18n
.getString("errorLaunchingShellText"),
780 // Setup the scroll bars
781 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
784 // Hide mouse when typing option
785 if (System
.getProperty("jexer.TTerminal.hideMouseWhenTyping",
786 "true").equals("false")) {
788 hideMouseWhenTyping
= false;
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() {
829 TApplication app
= getApplication();
831 if (closeAction
!= null) {
832 // We have to put this action inside invokeLater() because it
833 // could be executed during draw() when syncing with ECMA48.
834 app
.invokeLater(new Runnable() {
836 closeAction
.DO(TTerminalWidget
.this);
840 if (getApplication() != null) {
841 getApplication().postEvent(new TMenuEvent(
848 * Copy out variables from the emulator that TTerminal has to expose on
851 private void readEmulatorState() {
852 // Synchronize against the emulator so we don't stomp on its reader
854 synchronized (emulator
) {
856 setCursorX(emulator
.getCursorX());
857 setCursorY(emulator
.getCursorY()
858 + (getHeight() - emulator
.getHeight())
859 - getVerticalValue());
860 setCursorVisible(emulator
.isCursorVisible());
861 if (getCursorX() > getWidth()) {
862 setCursorVisible(false);
864 if ((getCursorY() >= getHeight()) || (getCursorY() < 0)) {
865 setCursorVisible(false);
867 if (emulator
.getScreenTitle().length() > 0) {
868 // Only update the title if the shell is still alive
870 title
= emulator
.getScreenTitle();
874 // Check to see if the shell has died.
875 if (!emulator
.isReading() && (shell
!= null)) {
877 int rc
= shell
.exitValue();
878 // The emulator exited on its own, all is fine
879 title
= MessageFormat
.format(i18n
.
880 getString("windowTitleCompleted"), title
, rc
);
885 } catch (IllegalThreadStateException e
) {
886 // The emulator thread has exited, but the shell Process
887 // hasn't figured that out yet. Do nothing, we will see
888 // this in a future tick.
890 } else if (emulator
.isReading() && (shell
!= null)) {
891 // The shell might be dead, let's check
893 int rc
= shell
.exitValue();
894 // If we got here, the shell died.
895 title
= MessageFormat
.format(i18n
.
896 getString("windowTitleCompleted"), title
, rc
);
901 } catch (IllegalThreadStateException e
) {
902 // The shell is still running, do nothing.
906 } // synchronized (emulator)
910 * Check if a mouse press/release/motion event coordinate is over the
913 * @param mouse a mouse-based event
914 * @return whether or not the mouse is on the emulator
916 private boolean mouseOnEmulator(final TMouseEvent mouse
) {
918 if (!emulator
.isReading()) {
922 if ((mouse
.getX() >= 0)
923 && (mouse
.getX() < getWidth() - 1)
924 && (mouse
.getY() >= 0)
925 && (mouse
.getY() < getHeight())
933 * Copy a display buffer.
935 * @param buffer the buffer to copy
936 * @return a deep copy of the buffer's data
938 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
939 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
940 for (DisplayLine line
: buffer
) {
941 result
.add(new DisplayLine(line
));
947 * Draw glyphs for a double-width or double-height VT100 cell to two
950 * @param line the line this VT100 cell is in
951 * @param x the X position to draw the left half to
952 * @param y the Y position to draw to
953 * @param cell the cell to draw
955 private void putDoubleWidthCharXY(final DisplayLine line
, final int x
,
956 final int y
, final Cell cell
) {
958 int textWidth
= getScreen().getTextWidth();
959 int textHeight
= getScreen().getTextHeight();
960 boolean cursorBlinkVisible
= true;
962 if (getScreen() instanceof SwingTerminal
) {
963 SwingTerminal terminal
= (SwingTerminal
) getScreen();
964 cursorBlinkVisible
= terminal
.getCursorBlinkVisible();
965 } else if (getScreen() instanceof ECMA48Terminal
) {
966 ECMA48Terminal terminal
= (ECMA48Terminal
) getScreen();
968 if (!terminal
.hasSixel()) {
969 // The backend does not have sixel support, draw this as text
971 putCharXY(x
, y
, cell
);
972 putCharXY(x
+ 1, y
, ' ', cell
);
975 cursorBlinkVisible
= blinkState
;
977 // We don't know how to dray glyphs to this screen, draw them as
978 // text and bail out.
979 putCharXY(x
, y
, cell
);
980 putCharXY(x
+ 1, y
, ' ', cell
);
984 if ((textWidth
!= lastTextWidth
) || (textHeight
!= lastTextHeight
)) {
985 // Screen size has changed, reset the font.
986 setupFont(textHeight
);
987 lastTextWidth
= textWidth
;
988 lastTextHeight
= textHeight
;
990 assert (doubleFont
!= null);
993 if (line
.getDoubleHeight() == 1) {
994 // Double-height top half: don't draw the underline.
995 Cell newCell
= new Cell(cell
);
996 newCell
.setUnderline(false);
997 image
= doubleFont
.getImage(newCell
, textWidth
* 2, textHeight
* 2,
1000 image
= doubleFont
.getImage(cell
, textWidth
* 2, textHeight
* 2,
1001 cursorBlinkVisible
);
1004 // Now that we have the double-wide glyph drawn, copy the right
1005 // pieces of it to the cells.
1006 Cell left
= new Cell(cell
);
1007 Cell right
= new Cell(cell
);
1009 BufferedImage leftImage
= null;
1010 BufferedImage rightImage
= null;
1012 System.err.println("image " + image + " textWidth " + textWidth +
1013 " textHeight " + textHeight);
1016 switch (line
.getDoubleHeight()) {
1018 // Top half double height
1019 leftImage
= image
.getSubimage(0, 0, textWidth
, textHeight
);
1020 rightImage
= image
.getSubimage(textWidth
, 0, textWidth
, textHeight
);
1023 // Bottom half double height
1024 leftImage
= image
.getSubimage(0, textHeight
, textWidth
, textHeight
);
1025 rightImage
= image
.getSubimage(textWidth
, textHeight
,
1026 textWidth
, textHeight
);
1029 // Either single height double-width, or error fallback
1030 BufferedImage wideImage
= new BufferedImage(textWidth
* 2,
1031 textHeight
, BufferedImage
.TYPE_INT_ARGB
);
1032 Graphics2D grWide
= wideImage
.createGraphics();
1033 grWide
.drawImage(image
, 0, 0, wideImage
.getWidth(),
1034 wideImage
.getHeight(), null);
1036 leftImage
= wideImage
.getSubimage(0, 0, textWidth
, textHeight
);
1037 rightImage
= wideImage
.getSubimage(textWidth
, 0, textWidth
,
1041 left
.setImage(leftImage
);
1042 right
.setImage(rightImage
);
1043 // Since we have image data, ditch the character here. Otherwise, a
1044 // drawBoxShadow() over the terminal window will show the characters
1045 // which looks wrong.
1048 putCharXY(x
, y
, left
);
1049 putCharXY(x
+ 1, y
, right
);
1053 * Set up the double-width font.
1055 * @param fontSize the size of font to request for the single-width font.
1056 * The double-width font will be 2x this value.
1058 private void setupFont(final int fontSize
) {
1059 doubleFont
= GlyphMaker
.getInstance(fontSize
* 2);
1061 // Special case: the ECMA48 backend needs to have a timer to drive
1063 if (getScreen() instanceof jexer
.backend
.ECMA48Terminal
) {
1065 // Blink every 500 millis.
1067 getApplication().addTimer(millis
, true,
1070 blinkState
= !blinkState
;
1071 getApplication().doRepaint();
1080 // ------------------------------------------------------------------------
1081 // DisplayListener --------------------------------------------------------
1082 // ------------------------------------------------------------------------
1085 * Called by emulator when fresh data has come in.
1087 public void displayChanged() {
1089 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
1093 * Function to call to obtain the display width.
1095 * @return the number of columns in the display
1097 public int getDisplayWidth() {
1105 * Function to call to obtain the display height.
1107 * @return the number of rows in the display
1109 public int getDisplayHeight() {