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
.TCommandEvent
;
53 import jexer
.event
.TKeypressEvent
;
54 import jexer
.event
.TMenuEvent
;
55 import jexer
.event
.TMouseEvent
;
56 import jexer
.event
.TResizeEvent
;
57 import jexer
.menu
.TMenu
;
58 import jexer
.tterminal
.DisplayLine
;
59 import jexer
.tterminal
.DisplayListener
;
60 import jexer
.tterminal
.ECMA48
;
61 import static jexer
.TCommand
.*;
62 import static jexer
.TKeypress
.*;
65 * TTerminalWidget exposes a ECMA-48 / ANSI X3.64 style terminal in a widget.
67 public class TTerminalWidget
extends TScrollableWidget
68 implements DisplayListener
, EditMenuUser
{
73 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TTerminalWidget
.class.getName());
75 // ------------------------------------------------------------------------
76 // Variables --------------------------------------------------------------
77 // ------------------------------------------------------------------------
82 private ECMA48 emulator
;
85 * The Process created by the shell spawning constructor.
87 private Process shell
;
90 * If true, we are using the ptypipe utility to support dynamic window
91 * resizing. ptypipe is available at
92 * https://gitlab.com/klamonte/ptypipe .
94 private boolean ptypipe
= false;
99 private GlyphMaker doubleFont
;
102 * Last text width value.
104 private int lastTextWidth
= -1;
107 * Last text height value.
109 private int lastTextHeight
= -1;
112 * The blink state, used only by ECMA48 backend and when double-width
113 * chars must be drawn.
115 private boolean blinkState
= true;
118 * Timer flag, used only by ECMA48 backend and when double-width chars
121 private boolean haveTimer
= false;
124 * The last seen visible display.
126 private List
<DisplayLine
> display
;
129 * If true, the display has changed and needs updating.
131 private volatile boolean dirty
= true;
134 * Time that the display was last updated.
136 private long lastUpdateTime
= 0;
139 * If true, hide the mouse after typing a keystroke.
141 private boolean hideMouseWhenTyping
= true;
144 * If true, the mouse should not be displayed because a keystroke was
147 private boolean typingHidMouse
= false;
150 * The return value from the emulator.
152 private int exitValue
= -1;
155 * Title to expose to a window.
157 private String title
= "";
160 * Action to perform when the terminal exits.
162 private TAction closeAction
= null;
164 // ------------------------------------------------------------------------
165 // Constructors -----------------------------------------------------------
166 // ------------------------------------------------------------------------
169 * Public constructor spawns a custom command line.
171 * @param parent parent widget
172 * @param x column relative to parent
173 * @param y row relative to parent
174 * @param commandLine the command line to execute
176 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
177 final String commandLine
) {
179 this(parent
, x
, y
, commandLine
.split("\\s+"));
183 * Public constructor spawns a custom command line.
185 * @param parent parent widget
186 * @param x column relative to parent
187 * @param y row relative to parent
188 * @param command the command line to execute
190 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
191 final String
[] command
) {
193 this(parent
, x
, y
, command
, null);
197 * Public constructor spawns a custom command line.
199 * @param parent parent widget
200 * @param x column relative to parent
201 * @param y row relative to parent
202 * @param command the command line to execute
203 * @param closeAction action to perform when the shell sxits
205 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
206 final String
[] command
, final TAction closeAction
) {
208 this(parent
, x
, y
, 80, 24, command
, closeAction
);
212 * Public constructor spawns a custom command line.
214 * @param parent parent widget
215 * @param x column relative to parent
216 * @param y row relative to parent
217 * @param width width of widget
218 * @param height height of widget
219 * @param command the command line to execute
220 * @param closeAction action to perform when the shell sxits
222 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
223 final int width
, final int height
, final String
[] command
,
224 final TAction closeAction
) {
226 super(parent
, x
, y
, width
, height
);
228 this.closeAction
= closeAction
;
230 String
[] fullCommand
;
232 // Spawn a shell and pass its I/O to the other constructor.
233 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
234 && (System
.getProperty("jexer.TTerminal.ptypipe").
238 fullCommand
= new String
[command
.length
+ 1];
239 fullCommand
[0] = "ptypipe";
240 System
.arraycopy(command
, 0, fullCommand
, 1, command
.length
);
241 } else if (System
.getProperty("os.name").startsWith("Windows")) {
242 fullCommand
= new String
[3];
243 fullCommand
[0] = "cmd";
244 fullCommand
[1] = "/c";
245 fullCommand
[2] = stringArrayToString(command
);
246 } else if (System
.getProperty("os.name").startsWith("Mac")) {
247 fullCommand
= new String
[6];
248 fullCommand
[0] = "script";
249 fullCommand
[1] = "-q";
250 fullCommand
[2] = "-F";
251 fullCommand
[3] = "/dev/null";
252 fullCommand
[4] = "-c";
253 fullCommand
[5] = stringArrayToString(command
);
255 // Default: behave like Linux
256 fullCommand
= new String
[5];
257 fullCommand
[0] = "script";
258 fullCommand
[1] = "-fqe";
259 fullCommand
[2] = "/dev/null";
260 fullCommand
[3] = "-c";
261 fullCommand
[4] = stringArrayToString(command
);
263 spawnShell(fullCommand
);
267 * Public constructor spawns a shell.
269 * @param parent parent widget
270 * @param x column relative to parent
271 * @param y row relative to parent
273 public TTerminalWidget(final TWidget parent
, final int x
, final int y
) {
274 this(parent
, x
, y
, (TAction
) null);
278 * Public constructor spawns a shell.
280 * @param parent parent widget
281 * @param x column relative to parent
282 * @param y row relative to parent
283 * @param closeAction action to perform when the shell sxits
285 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
286 final TAction closeAction
) {
288 this(parent
, x
, y
, 80, 24, closeAction
);
292 * Public constructor spawns a shell.
294 * @param parent parent widget
295 * @param x column relative to parent
296 * @param y row relative to parent
297 * @param width width of widget
298 * @param height height of widget
299 * @param closeAction action to perform when the shell sxits
301 public TTerminalWidget(final TWidget parent
, final int x
, final int y
,
302 final int width
, final int height
, final TAction closeAction
) {
304 super(parent
, x
, y
, width
, height
);
306 this.closeAction
= closeAction
;
308 if (System
.getProperty("jexer.TTerminal.shell") != null) {
309 String shell
= System
.getProperty("jexer.TTerminal.shell");
310 if (shell
.trim().startsWith("ptypipe")) {
313 spawnShell(shell
.split("\\s+"));
317 String cmdShellWindows
= "cmd.exe";
319 // You cannot run a login shell in a bare Process interactively, due
320 // to libc's behavior of buffering when stdin/stdout aren't a tty.
321 // Use 'script' instead to run a shell in a pty. And because BSD and
322 // GNU differ on the '-f' vs '-F' flags, we need two different
324 String cmdShellGNU
= "script -fqe /dev/null";
325 String cmdShellBSD
= "script -q -F /dev/null";
327 // ptypipe is another solution that permits dynamic window resizing.
328 String cmdShellPtypipe
= "ptypipe /bin/bash --login";
330 // Spawn a shell and pass its I/O to the other constructor.
331 if ((System
.getProperty("jexer.TTerminal.ptypipe") != null)
332 && (System
.getProperty("jexer.TTerminal.ptypipe").
336 spawnShell(cmdShellPtypipe
.split("\\s+"));
337 } else if (System
.getProperty("os.name").startsWith("Windows")) {
338 spawnShell(cmdShellWindows
.split("\\s+"));
339 } else if (System
.getProperty("os.name").startsWith("Mac")) {
340 spawnShell(cmdShellBSD
.split("\\s+"));
341 } else if (System
.getProperty("os.name").startsWith("Linux")) {
342 spawnShell(cmdShellGNU
.split("\\s+"));
344 // When all else fails, assume GNU.
345 spawnShell(cmdShellGNU
.split("\\s+"));
349 // ------------------------------------------------------------------------
350 // Event handlers ---------------------------------------------------------
351 // ------------------------------------------------------------------------
354 * Handle window/screen resize events.
356 * @param resize resize event
359 public void onResize(final TResizeEvent resize
) {
360 // Let TWidget set my size.
361 super.onResize(resize
);
363 if (emulator
== null) {
367 // Synchronize against the emulator so we don't stomp on its reader
369 synchronized (emulator
) {
371 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
372 // Resize the scroll bars
376 // Get out of scrollback
380 emulator
.setWidth(getWidth());
381 emulator
.setHeight(getHeight());
383 emulator
.writeRemote("\033[8;" + getHeight() + ";" +
387 // Pass the correct text cell width/height to the emulator
388 if (getScreen() != null) {
389 emulator
.setTextWidth(getScreen().getTextWidth());
390 emulator
.setTextHeight(getScreen().getTextHeight());
395 } // synchronized (emulator)
401 * @param keypress keystroke event
404 public void onKeypress(final TKeypressEvent keypress
) {
405 if (hideMouseWhenTyping
) {
406 typingHidMouse
= true;
409 // Scrollback up/down
410 if (keypress
.equals(kbShiftPgUp
)
411 || keypress
.equals(kbCtrlPgUp
)
412 || keypress
.equals(kbAltPgUp
)
414 bigVerticalDecrement();
418 if (keypress
.equals(kbShiftPgDn
)
419 || keypress
.equals(kbCtrlPgDn
)
420 || keypress
.equals(kbAltPgDn
)
422 bigVerticalIncrement();
427 if ((emulator
!= null) && (emulator
.isReading())) {
428 // Get out of scrollback
430 emulator
.addUserEvent(keypress
);
432 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
433 // this is kBEnter then also send kbCtrlJ.
434 if (keypress
.equals(kbEnter
)) {
435 if (System
.getProperty("os.name").startsWith("Windows")
436 && (System
.getProperty("jexer.TTerminal.cmdHack",
437 "true").equals("true"))
439 emulator
.addUserEvent(new TKeypressEvent(kbCtrlJ
));
447 // Process is closed, honor "normal" TUI keystrokes
448 super.onKeypress(keypress
);
452 * Handle mouse press events.
454 * @param mouse mouse button press event
457 public void onMouseDown(final TMouseEvent mouse
) {
458 if (hideMouseWhenTyping
) {
459 typingHidMouse
= false;
462 if (emulator
!= null) {
463 // If the emulator is tracking mouse buttons, it needs to see
465 if (emulator
.getMouseProtocol() == ECMA48
.MouseProtocol
.OFF
) {
466 if (mouse
.isMouseWheelUp()) {
471 if (mouse
.isMouseWheelDown()) {
477 if (mouseOnEmulator(mouse
)) {
478 emulator
.addUserEvent(mouse
);
484 // Emulator didn't consume it, pass it on
485 super.onMouseDown(mouse
);
489 * Handle mouse release events.
491 * @param mouse mouse button release event
494 public void onMouseUp(final TMouseEvent mouse
) {
495 if (hideMouseWhenTyping
) {
496 typingHidMouse
= false;
499 if ((emulator
!= null) && (mouseOnEmulator(mouse
))) {
500 emulator
.addUserEvent(mouse
);
505 // Emulator didn't consume it, pass it on
506 super.onMouseUp(mouse
);
510 * Handle mouse motion events.
512 * @param mouse mouse motion event
515 public void onMouseMotion(final TMouseEvent mouse
) {
516 if (hideMouseWhenTyping
) {
517 typingHidMouse
= false;
520 if ((emulator
!= null) && (mouseOnEmulator(mouse
))) {
521 emulator
.addUserEvent(mouse
);
526 // Emulator didn't consume it, pass it on
527 super.onMouseMotion(mouse
);
531 * Handle posted command events.
533 * @param command command event
536 public void onCommand(final TCommandEvent command
) {
537 if (emulator
== null) {
541 if (command
.equals(cmPaste
)) {
542 // Paste text from clipboard.
543 String text
= getClipboard().pasteText();
545 for (int i
= 0; i
< text
.length(); ) {
546 int ch
= text
.codePointAt(i
);
547 emulator
.addUserEvent(new TKeypressEvent(false, 0, ch
,
548 false, false, false));
549 i
+= Character
.charCount(ch
);
556 // ------------------------------------------------------------------------
557 // TScrollableWidget ------------------------------------------------------
558 // ------------------------------------------------------------------------
561 * Draw the display buffer.
565 if (emulator
== null) {
569 int width
= getDisplayWidth();
571 boolean syncEmulator
= false;
572 if (System
.currentTimeMillis() - lastUpdateTime
>= 50) {
573 // Too much time has passed, draw it all.
575 } else if (emulator
.isReading() && (dirty
== false)) {
576 // Wait until the emulator has brought more data in.
577 syncEmulator
= false;
578 } else if (!emulator
.isReading() && (dirty
== true)) {
579 // The emulator won't receive more data, update the display.
583 if ((syncEmulator
== true)
586 // We want to minimize the amount of time we have the emulator
587 // locked. Grab a copy of its display.
588 synchronized (emulator
) {
589 // Update the scroll bars
593 // We lost the connection, onShellExit() called an action
594 // that ultimately removed this widget from the UI
595 // hierarchy, so no one cares if we update the display.
600 if ((display
== null) || emulator
.isReading()) {
601 display
= emulator
.getVisibleDisplay(getHeight(),
602 -getVerticalValue());
603 assert (display
.size() == getHeight());
605 width
= emulator
.getWidth();
610 // Now draw the emulator screen
612 for (DisplayLine line
: display
) {
613 int widthMax
= width
;
614 if (line
.isDoubleWidth()) {
617 if (widthMax
> getWidth()) {
618 widthMax
= getWidth();
620 for (int i
= 0; i
< widthMax
; i
++) {
621 Cell ch
= line
.charAt(i
);
624 putCharXY(i
, row
, ch
);
628 Cell newCell
= new Cell(ch
);
629 boolean reverse
= line
.isReverseColor() ^ ch
.isReverse();
630 newCell
.setReverse(false);
632 if (ch
.getForeColorRGB() < 0) {
633 newCell
.setBackColor(ch
.getForeColor());
634 newCell
.setBackColorRGB(-1);
636 newCell
.setBackColorRGB(ch
.getForeColorRGB());
638 if (ch
.getBackColorRGB() < 0) {
639 newCell
.setForeColor(ch
.getBackColor());
640 newCell
.setForeColorRGB(-1);
642 newCell
.setForeColorRGB(ch
.getBackColorRGB());
645 if (line
.isDoubleWidth()) {
646 putDoubleWidthCharXY(line
, (i
* 2), row
, newCell
);
648 putCharXY(i
, row
, newCell
);
656 * Set current value of the vertical scroll.
658 * @param value the new scroll value
661 public void setVerticalValue(final int value
) {
662 super.setVerticalValue(value
);
667 * Perform a small step change up.
670 public void verticalDecrement() {
671 super.verticalDecrement();
676 * Perform a small step change down.
679 public void verticalIncrement() {
680 super.verticalIncrement();
685 * Perform a big step change up.
687 public void bigVerticalDecrement() {
688 super.bigVerticalDecrement();
693 * Perform a big step change down.
695 public void bigVerticalIncrement() {
696 super.bigVerticalIncrement();
701 * Go to the top edge of the vertical scroller.
703 public void toTop() {
709 * Go to the bottom edge of the vertical scroller.
711 public void toBottom() {
717 * Handle widget close.
720 public void close() {
721 if (emulator
!= null) {
725 terminateShellChildProcess();
732 * Resize scrollbars for a new width/height.
735 public void reflowData() {
736 if (emulator
== null) {
740 // Synchronize against the emulator so we don't stomp on its reader
742 synchronized (emulator
) {
744 // Pull cursor information
747 // Vertical scrollbar
748 setTopValue(getHeight()
749 - (emulator
.getScrollbackBuffer().size()
750 + emulator
.getDisplayBuffer().size()));
751 setVerticalBigChange(getHeight());
753 } // synchronized (emulator)
756 // ------------------------------------------------------------------------
757 // TTerminalWidget --------------------------------------------------------
758 // ------------------------------------------------------------------------
761 * Get the desired window title.
765 public String
getTitle() {
770 * Returns true if this widget does not want the application-wide mouse
771 * cursor drawn over it.
773 * @return true if this widget does not want the application-wide mouse
774 * cursor drawn over it
776 public boolean hasHiddenMouse() {
777 if (emulator
== null) {
780 return (emulator
.hasHiddenMousePointer() || typingHidMouse
);
784 * See if the terminal is still running.
786 * @return if true, we are still connected to / reading from the remote
789 public boolean isReading() {
790 if (emulator
== null) {
793 return emulator
.isReading();
797 * Convert a string array to a whitespace-separated string.
799 * @param array the string array
800 * @return a single string
802 private String
stringArrayToString(final String
[] array
) {
803 StringBuilder sb
= new StringBuilder(array
[0].length());
804 for (int i
= 0; i
< array
.length
; i
++) {
806 if (i
< array
.length
- 1) {
810 return sb
.toString();
816 * @param command the command line to execute
818 private void spawnShell(final String
[] command
) {
821 System.err.printf("spawnShell(): '%s'\n",
822 stringArrayToString(command));
825 // We will have vScroller for its data fields and mouse event
826 // handling, but do not want to draw it.
827 vScroller
= new TVScroller(null, getWidth(), 0, getHeight());
828 vScroller
.setVisible(false);
831 title
= i18n
.getString("windowTitle");
834 ECMA48
.DeviceType deviceType
= ECMA48
.DeviceType
.XTERM
;
837 ProcessBuilder pb
= new ProcessBuilder(command
);
838 Map
<String
, String
> env
= pb
.environment();
839 env
.put("TERM", ECMA48
.deviceTypeTerm(deviceType
));
840 env
.put("LANG", ECMA48
.deviceTypeLang(deviceType
, "en"));
841 env
.put("COLUMNS", "80");
842 env
.put("LINES", "24");
843 pb
.redirectErrorStream(true);
845 emulator
= new ECMA48(deviceType
, shell
.getInputStream(),
846 shell
.getOutputStream(), this);
847 } catch (IOException e
) {
848 messageBox(i18n
.getString("errorLaunchingShellTitle"),
849 MessageFormat
.format(i18n
.getString("errorLaunchingShellText"),
853 // Setup the scroll bars
854 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
857 // Hide mouse when typing option
858 if (System
.getProperty("jexer.TTerminal.hideMouseWhenTyping",
859 "true").equals("false")) {
861 hideMouseWhenTyping
= false;
866 * Terminate the child of the 'script' process used on POSIX. This may
869 private void terminateShellChildProcess() {
871 if (shell
.getClass().getName().equals("java.lang.UNIXProcess")) {
872 /* get the PID on unix/linux systems */
874 Field field
= shell
.getClass().getDeclaredField("pid");
875 field
.setAccessible(true);
876 pid
= field
.getInt(shell
);
877 } catch (Throwable e
) {
878 // SQUASH, this didn't work. Just bail out quietly.
883 // shell.destroy() works successfully at killing this side of
884 // 'script'. But we need to make sure the other side (child
885 // process) is also killed.
886 String
[] cmdKillIt
= {
887 "pkill", "-P", Integer
.toString(pid
)
890 Runtime
.getRuntime().exec(cmdKillIt
);
891 } catch (Throwable e
) {
892 // SQUASH, this didn't work. Just bail out quietly.
899 * Hook for subclasses to be notified of the shell termination.
901 public void onShellExit() {
902 TApplication app
= getApplication();
904 if (closeAction
!= null) {
905 // We have to put this action inside invokeLater() because it
906 // could be executed during draw() when syncing with ECMA48.
907 app
.invokeLater(new Runnable() {
909 closeAction
.DO(TTerminalWidget
.this);
913 if (getApplication() != null) {
914 getApplication().postEvent(new TMenuEvent(
921 * Copy out variables from the emulator that TTerminal has to expose on
924 private void readEmulatorState() {
925 if (emulator
== null) {
929 // Synchronize against the emulator so we don't stomp on its reader
931 synchronized (emulator
) {
933 setCursorX(emulator
.getCursorX());
934 setCursorY(emulator
.getCursorY()
935 + (getHeight() - emulator
.getHeight())
936 - getVerticalValue());
937 setCursorVisible(emulator
.isCursorVisible());
938 if (getCursorX() > getWidth()) {
939 setCursorVisible(false);
941 if ((getCursorY() >= getHeight()) || (getCursorY() < 0)) {
942 setCursorVisible(false);
944 if (emulator
.getScreenTitle().length() > 0) {
945 // Only update the title if the shell is still alive
947 title
= emulator
.getScreenTitle();
951 // Check to see if the shell has died.
952 if (!emulator
.isReading() && (shell
!= null)) {
954 int rc
= shell
.exitValue();
955 // The emulator exited on its own, all is fine
956 title
= MessageFormat
.format(i18n
.
957 getString("windowTitleCompleted"), title
, rc
);
962 } catch (IllegalThreadStateException e
) {
963 // The emulator thread has exited, but the shell Process
964 // hasn't figured that out yet. Do nothing, we will see
965 // this in a future tick.
967 } else if (emulator
.isReading() && (shell
!= null)) {
968 // The shell might be dead, let's check
970 int rc
= shell
.exitValue();
971 // If we got here, the shell died.
972 title
= MessageFormat
.format(i18n
.
973 getString("windowTitleCompleted"), title
, rc
);
978 } catch (IllegalThreadStateException e
) {
979 // The shell is still running, do nothing.
983 } // synchronized (emulator)
987 * Check if a mouse press/release/motion event coordinate is over the
990 * @param mouse a mouse-based event
991 * @return whether or not the mouse is on the emulator
993 private boolean mouseOnEmulator(final TMouseEvent mouse
) {
994 if (emulator
== null) {
998 if (!emulator
.isReading()) {
1002 if ((mouse
.getX() >= 0)
1003 && (mouse
.getX() < getWidth() - 1)
1004 && (mouse
.getY() >= 0)
1005 && (mouse
.getY() < getHeight())
1013 * Draw glyphs for a double-width or double-height VT100 cell to two
1016 * @param line the line this VT100 cell is in
1017 * @param x the X position to draw the left half to
1018 * @param y the Y position to draw to
1019 * @param cell the cell to draw
1021 private void putDoubleWidthCharXY(final DisplayLine line
, final int x
,
1022 final int y
, final Cell cell
) {
1024 int textWidth
= getScreen().getTextWidth();
1025 int textHeight
= getScreen().getTextHeight();
1026 boolean cursorBlinkVisible
= true;
1028 if (getScreen() instanceof SwingTerminal
) {
1029 SwingTerminal terminal
= (SwingTerminal
) getScreen();
1030 cursorBlinkVisible
= terminal
.getCursorBlinkVisible();
1031 } else if (getScreen() instanceof ECMA48Terminal
) {
1032 ECMA48Terminal terminal
= (ECMA48Terminal
) getScreen();
1034 if (!terminal
.hasSixel()) {
1035 // The backend does not have sixel support, draw this as text
1037 putCharXY(x
, y
, cell
);
1038 putCharXY(x
+ 1, y
, ' ', cell
);
1041 cursorBlinkVisible
= blinkState
;
1043 // We don't know how to dray glyphs to this screen, draw them as
1044 // text and bail out.
1045 putCharXY(x
, y
, cell
);
1046 putCharXY(x
+ 1, y
, ' ', cell
);
1050 if ((textWidth
!= lastTextWidth
) || (textHeight
!= lastTextHeight
)) {
1051 // Screen size has changed, reset the font.
1052 setupFont(textHeight
);
1053 lastTextWidth
= textWidth
;
1054 lastTextHeight
= textHeight
;
1056 assert (doubleFont
!= null);
1058 BufferedImage image
;
1059 if (line
.getDoubleHeight() == 1) {
1060 // Double-height top half: don't draw the underline.
1061 Cell newCell
= new Cell(cell
);
1062 newCell
.setUnderline(false);
1063 image
= doubleFont
.getImage(newCell
, textWidth
* 2, textHeight
* 2,
1064 cursorBlinkVisible
);
1066 image
= doubleFont
.getImage(cell
, textWidth
* 2, textHeight
* 2,
1067 cursorBlinkVisible
);
1070 // Now that we have the double-wide glyph drawn, copy the right
1071 // pieces of it to the cells.
1072 Cell left
= new Cell(cell
);
1073 Cell right
= new Cell(cell
);
1075 BufferedImage leftImage
= null;
1076 BufferedImage rightImage
= null;
1078 System.err.println("image " + image + " textWidth " + textWidth +
1079 " textHeight " + textHeight);
1082 switch (line
.getDoubleHeight()) {
1084 // Top half double height
1085 leftImage
= image
.getSubimage(0, 0, textWidth
, textHeight
);
1086 rightImage
= image
.getSubimage(textWidth
, 0, textWidth
, textHeight
);
1089 // Bottom half double height
1090 leftImage
= image
.getSubimage(0, textHeight
, textWidth
, textHeight
);
1091 rightImage
= image
.getSubimage(textWidth
, textHeight
,
1092 textWidth
, textHeight
);
1095 // Either single height double-width, or error fallback
1096 BufferedImage wideImage
= new BufferedImage(textWidth
* 2,
1097 textHeight
, BufferedImage
.TYPE_INT_ARGB
);
1098 Graphics2D grWide
= wideImage
.createGraphics();
1099 grWide
.drawImage(image
, 0, 0, wideImage
.getWidth(),
1100 wideImage
.getHeight(), null);
1102 leftImage
= wideImage
.getSubimage(0, 0, textWidth
, textHeight
);
1103 rightImage
= wideImage
.getSubimage(textWidth
, 0, textWidth
,
1107 left
.setImage(leftImage
);
1108 right
.setImage(rightImage
);
1109 // Since we have image data, ditch the character here. Otherwise, a
1110 // drawBoxShadow() over the terminal window will show the characters
1111 // which looks wrong.
1114 putCharXY(x
, y
, left
);
1115 putCharXY(x
+ 1, y
, right
);
1119 * Set up the double-width font.
1121 * @param fontSize the size of font to request for the single-width font.
1122 * The double-width font will be 2x this value.
1124 private void setupFont(final int fontSize
) {
1125 doubleFont
= GlyphMaker
.getInstance(fontSize
* 2);
1127 // Special case: the ECMA48 backend needs to have a timer to drive
1129 if (getScreen() instanceof jexer
.backend
.ECMA48Terminal
) {
1131 // Blink every 500 millis.
1133 getApplication().addTimer(millis
, true,
1136 blinkState
= !blinkState
;
1137 getApplication().doRepaint();
1146 // ------------------------------------------------------------------------
1147 // DisplayListener --------------------------------------------------------
1148 // ------------------------------------------------------------------------
1151 * Called by emulator when fresh data has come in.
1153 public void displayChanged() {
1154 if (emulator
!= null) {
1155 // Force sync here: EMCA48.run() thread might be setting
1156 // dirty=true while TTerminalWdiget.draw() is setting
1157 // dirty=false. If these writes start interleaving, the display
1158 // stops getting updated.
1159 synchronized (emulator
) {
1165 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
1169 * Function to call to obtain the display width.
1171 * @return the number of columns in the display
1173 public int getDisplayWidth() {
1181 * Function to call to obtain the display height.
1183 * @return the number of rows in the display
1185 public int getDisplayHeight() {
1192 // ------------------------------------------------------------------------
1193 // EditMenuUser -----------------------------------------------------------
1194 // ------------------------------------------------------------------------
1197 * Check if the cut menu item should be enabled.
1199 * @return true if the cut menu item should be enabled
1201 public boolean isEditMenuCut() {
1206 * Check if the copy menu item should be enabled.
1208 * @return true if the copy menu item should be enabled
1210 public boolean isEditMenuCopy() {
1215 * Check if the paste menu item should be enabled.
1217 * @return true if the paste menu item should be enabled
1219 public boolean isEditMenuPaste() {
1224 * Check if the clear menu item should be enabled.
1226 * @return true if the clear menu item should be enabled
1228 public boolean isEditMenuClear() {