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
387 if ((scrollback
== null) || emulator
.isReading()) {
388 scrollback
= copyBuffer(emulator
.getScrollbackBuffer());
389 display
= copyBuffer(emulator
.getDisplayBuffer());
391 width
= emulator
.getWidth();
396 // Put together the visible rows
397 int visibleHeight
= getHeight();
398 int visibleBottom
= scrollback
.size() + display
.size()
399 + getVerticalValue();
400 assert (visibleBottom
>= 0);
402 List
<DisplayLine
> preceedingBlankLines
= new ArrayList
<DisplayLine
>();
403 int visibleTop
= visibleBottom
- visibleHeight
;
404 if (visibleTop
< 0) {
405 for (int i
= visibleTop
; i
< 0; i
++) {
406 preceedingBlankLines
.add(emulator
.getBlankDisplayLine());
410 assert (visibleTop
>= 0);
412 List
<DisplayLine
> displayLines
= new ArrayList
<DisplayLine
>();
413 displayLines
.addAll(scrollback
);
414 displayLines
.addAll(display
);
416 List
<DisplayLine
> visibleLines
= new ArrayList
<DisplayLine
>();
417 visibleLines
.addAll(preceedingBlankLines
);
418 visibleLines
.addAll(displayLines
.subList(visibleTop
,
421 visibleHeight
-= visibleLines
.size();
422 assert (visibleHeight
>= 0);
424 // Now draw the emulator screen
426 for (DisplayLine line
: visibleLines
) {
427 int widthMax
= width
;
428 if (line
.isDoubleWidth()) {
431 if (widthMax
> getWidth()) {
432 widthMax
= getWidth();
434 for (int i
= 0; i
< widthMax
; i
++) {
435 Cell ch
= line
.charAt(i
);
438 putCharXY(i
, row
, ch
);
442 Cell newCell
= new Cell(ch
);
443 boolean reverse
= line
.isReverseColor() ^ ch
.isReverse();
444 newCell
.setReverse(false);
446 if (ch
.getForeColorRGB() < 0) {
447 newCell
.setBackColor(ch
.getForeColor());
448 newCell
.setBackColorRGB(-1);
450 newCell
.setBackColorRGB(ch
.getForeColorRGB());
452 if (ch
.getBackColorRGB() < 0) {
453 newCell
.setForeColor(ch
.getBackColor());
454 newCell
.setForeColorRGB(-1);
456 newCell
.setForeColorRGB(ch
.getBackColorRGB());
459 if (line
.isDoubleWidth()) {
460 putDoubleWidthCharXY(line
, (i
* 2), row
, newCell
);
462 putCharXY(i
, row
, newCell
);
466 if (row
== getHeight()) {
467 // Don't overwrite the box edge
471 CellAttributes background
= new CellAttributes();
472 // Fill in the blank lines on bottom
473 for (int i
= 0; i
< visibleHeight
; i
++) {
474 hLineXY(0, i
+ row
, getWidth(), ' ', background
);
480 * Handle widget close.
483 public void close() {
486 terminateShellChildProcess();
493 * Handle window/screen resize events.
495 * @param resize resize event
498 public void onResize(final TResizeEvent resize
) {
499 // Let TWidget set my size.
500 super.onResize(resize
);
502 // Synchronize against the emulator so we don't stomp on its reader
504 synchronized (emulator
) {
506 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
507 // Resize the scroll bars
511 // Get out of scrollback
515 emulator
.setWidth(getWidth());
516 emulator
.setHeight(getHeight());
518 emulator
.writeRemote("\033[8;" + getHeight() + ";" +
522 // Pass the correct text cell width/height to the emulator
523 if (getScreen() != null) {
524 emulator
.setTextWidth(getScreen().getTextWidth());
525 emulator
.setTextHeight(getScreen().getTextHeight());
530 } // synchronized (emulator)
534 * Resize scrollbars for a new width/height.
537 public void reflowData() {
539 // Synchronize against the emulator so we don't stomp on its reader
541 synchronized (emulator
) {
543 // Pull cursor information
546 // Vertical scrollbar
547 setTopValue(getHeight()
548 - (emulator
.getScrollbackBuffer().size()
549 + emulator
.getDisplayBuffer().size()));
550 setVerticalBigChange(getHeight());
552 } // synchronized (emulator)
558 * @param keypress keystroke event
561 public void onKeypress(final TKeypressEvent keypress
) {
562 if (hideMouseWhenTyping
) {
563 typingHidMouse
= true;
566 // Scrollback up/down
567 if (keypress
.equals(kbShiftPgUp
)
568 || keypress
.equals(kbCtrlPgUp
)
569 || keypress
.equals(kbAltPgUp
)
571 bigVerticalDecrement();
574 if (keypress
.equals(kbShiftPgDn
)
575 || keypress
.equals(kbCtrlPgDn
)
576 || keypress
.equals(kbAltPgDn
)
578 bigVerticalIncrement();
582 if (emulator
.isReading()) {
583 // Get out of scrollback
585 emulator
.addUserEvent(keypress
);
587 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
588 // this is kBEnter then also send kbCtrlJ.
589 if (keypress
.equals(kbEnter
)) {
590 if (System
.getProperty("os.name").startsWith("Windows")
591 && (System
.getProperty("jexer.TTerminal.cmdHack",
592 "true").equals("true"))
594 emulator
.addUserEvent(new TKeypressEvent(kbCtrlJ
));
602 // Process is closed, honor "normal" TUI keystrokes
603 super.onKeypress(keypress
);
607 * Handle mouse press events.
609 * @param mouse mouse button press event
612 public void onMouseDown(final TMouseEvent mouse
) {
613 if (hideMouseWhenTyping
) {
614 typingHidMouse
= false;
617 // If the emulator is tracking mouse buttons, it needs to see wheel
619 if (emulator
.getMouseProtocol() == ECMA48
.MouseProtocol
.OFF
) {
620 if (mouse
.isMouseWheelUp()) {
624 if (mouse
.isMouseWheelDown()) {
629 if (mouseOnEmulator(mouse
)) {
630 emulator
.addUserEvent(mouse
);
635 // Emulator didn't consume it, pass it on
636 super.onMouseDown(mouse
);
640 * Handle mouse release events.
642 * @param mouse mouse button release event
645 public void onMouseUp(final TMouseEvent mouse
) {
646 if (hideMouseWhenTyping
) {
647 typingHidMouse
= false;
650 if (mouseOnEmulator(mouse
)) {
651 emulator
.addUserEvent(mouse
);
656 // Emulator didn't consume it, pass it on
657 super.onMouseUp(mouse
);
661 * Handle mouse motion events.
663 * @param mouse mouse motion event
666 public void onMouseMotion(final TMouseEvent mouse
) {
667 if (hideMouseWhenTyping
) {
668 typingHidMouse
= false;
671 if (mouseOnEmulator(mouse
)) {
672 emulator
.addUserEvent(mouse
);
677 // Emulator didn't consume it, pass it on
678 super.onMouseMotion(mouse
);
681 // ------------------------------------------------------------------------
682 // TTerminalWidget --------------------------------------------------------
683 // ------------------------------------------------------------------------
686 * Get the desired window title.
690 public String
getTitle() {
695 * Returns true if this widget does not want the application-wide mouse
696 * cursor drawn over it.
698 * @return true if this widget does not want the application-wide mouse
699 * cursor drawn over it
701 public boolean hasHiddenMouse() {
702 return (emulator
.hasHiddenMousePointer() || typingHidMouse
);
706 * See if the terminal is still running.
708 * @return if true, we are still connected to / reading from the remote
711 public boolean isReading() {
712 return emulator
.isReading();
716 * Convert a string array to a whitespace-separated string.
718 * @param array the string array
719 * @return a single string
721 private String
stringArrayToString(final String
[] array
) {
722 StringBuilder sb
= new StringBuilder(array
[0].length());
723 for (int i
= 0; i
< array
.length
; i
++) {
725 if (i
< array
.length
- 1) {
729 return sb
.toString();
735 * @param command the command line to execute
737 private void spawnShell(final String
[] command
) {
740 System.err.printf("spawnShell(): '%s'\n",
741 stringArrayToString(command));
744 // We will have vScroller for its data fields and mouse event
745 // handling, but do not want to draw it.
746 vScroller
= new TVScroller(null, getWidth(), 0, getHeight());
747 vScroller
.setVisible(false);
750 title
= i18n
.getString("windowTitle");
753 ECMA48
.DeviceType deviceType
= ECMA48
.DeviceType
.XTERM
;
756 ProcessBuilder pb
= new ProcessBuilder(command
);
757 Map
<String
, String
> env
= pb
.environment();
758 env
.put("TERM", ECMA48
.deviceTypeTerm(deviceType
));
759 env
.put("LANG", ECMA48
.deviceTypeLang(deviceType
, "en"));
760 env
.put("COLUMNS", "80");
761 env
.put("LINES", "24");
762 pb
.redirectErrorStream(true);
764 emulator
= new ECMA48(deviceType
, shell
.getInputStream(),
765 shell
.getOutputStream(), this);
766 } catch (IOException e
) {
767 messageBox(i18n
.getString("errorLaunchingShellTitle"),
768 MessageFormat
.format(i18n
.getString("errorLaunchingShellText"),
772 // Setup the scroll bars
773 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
776 // Hide mouse when typing option
777 if (System
.getProperty("jexer.TTerminal.hideMouseWhenTyping",
778 "true").equals("false")) {
780 hideMouseWhenTyping
= false;
785 * Terminate the child of the 'script' process used on POSIX. This may
788 private void terminateShellChildProcess() {
790 if (shell
.getClass().getName().equals("java.lang.UNIXProcess")) {
791 /* get the PID on unix/linux systems */
793 Field field
= shell
.getClass().getDeclaredField("pid");
794 field
.setAccessible(true);
795 pid
= field
.getInt(shell
);
796 } catch (Throwable e
) {
797 // SQUASH, this didn't work. Just bail out quietly.
802 // shell.destroy() works successfully at killing this side of
803 // 'script'. But we need to make sure the other side (child
804 // process) is also killed.
805 String
[] cmdKillIt
= {
806 "pkill", "-P", Integer
.toString(pid
)
809 Runtime
.getRuntime().exec(cmdKillIt
);
810 } catch (Throwable e
) {
811 // SQUASH, this didn't work. Just bail out quietly.
818 * Hook for subclasses to be notified of the shell termination.
820 public void onShellExit() {
821 TApplication app
= getApplication();
823 app
.invokeLater(new Runnable() {
825 if (closeAction
!= null) {
828 if (getApplication() != null) {
829 getApplication().postEvent(new TMenuEvent(
838 * Copy out variables from the emulator that TTerminal has to expose on
841 private void readEmulatorState() {
842 // Synchronize against the emulator so we don't stomp on its reader
844 synchronized (emulator
) {
846 setCursorX(emulator
.getCursorX());
847 setCursorY(emulator
.getCursorY()
848 + (getHeight() - emulator
.getHeight())
849 - getVerticalValue());
850 setCursorVisible(emulator
.isCursorVisible());
851 if (getCursorX() > getWidth()) {
852 setCursorVisible(false);
854 if ((getCursorY() >= getHeight()) || (getCursorY() < 0)) {
855 setCursorVisible(false);
857 if (emulator
.getScreenTitle().length() > 0) {
858 // Only update the title if the shell is still alive
860 title
= emulator
.getScreenTitle();
864 // Check to see if the shell has died.
865 if (!emulator
.isReading() && (shell
!= null)) {
867 int rc
= shell
.exitValue();
868 // The emulator exited on its own, all is fine
869 title
= MessageFormat
.format(i18n
.
870 getString("windowTitleCompleted"), title
, rc
);
875 } catch (IllegalThreadStateException e
) {
876 // The emulator thread has exited, but the shell Process
877 // hasn't figured that out yet. Do nothing, we will see
878 // this in a future tick.
880 } else if (emulator
.isReading() && (shell
!= null)) {
881 // The shell might be dead, let's check
883 int rc
= shell
.exitValue();
884 // If we got here, the shell died.
885 title
= MessageFormat
.format(i18n
.
886 getString("windowTitleCompleted"), title
, rc
);
891 } catch (IllegalThreadStateException e
) {
892 // The shell is still running, do nothing.
896 } // synchronized (emulator)
900 * Check if a mouse press/release/motion event coordinate is over the
903 * @param mouse a mouse-based event
904 * @return whether or not the mouse is on the emulator
906 private boolean mouseOnEmulator(final TMouseEvent mouse
) {
908 if (!emulator
.isReading()) {
912 if ((mouse
.getX() >= 0)
913 && (mouse
.getX() < getWidth() - 1)
914 && (mouse
.getY() >= 0)
915 && (mouse
.getY() < getHeight())
923 * Copy a display buffer.
925 * @param buffer the buffer to copy
926 * @return a deep copy of the buffer's data
928 private List
<DisplayLine
> copyBuffer(final List
<DisplayLine
> buffer
) {
929 ArrayList
<DisplayLine
> result
= new ArrayList
<DisplayLine
>(buffer
.size());
930 for (DisplayLine line
: buffer
) {
931 result
.add(new DisplayLine(line
));
937 * Draw glyphs for a double-width or double-height VT100 cell to two
940 * @param line the line this VT100 cell is in
941 * @param x the X position to draw the left half to
942 * @param y the Y position to draw to
943 * @param cell the cell to draw
945 private void putDoubleWidthCharXY(final DisplayLine line
, final int x
,
946 final int y
, final Cell cell
) {
948 int textWidth
= getScreen().getTextWidth();
949 int textHeight
= getScreen().getTextHeight();
950 boolean cursorBlinkVisible
= true;
952 if (getScreen() instanceof SwingTerminal
) {
953 SwingTerminal terminal
= (SwingTerminal
) getScreen();
954 cursorBlinkVisible
= terminal
.getCursorBlinkVisible();
955 } else if (getScreen() instanceof ECMA48Terminal
) {
956 ECMA48Terminal terminal
= (ECMA48Terminal
) getScreen();
958 if (!terminal
.hasSixel()) {
959 // The backend does not have sixel support, draw this as text
961 putCharXY(x
, y
, cell
);
962 putCharXY(x
+ 1, y
, ' ', cell
);
965 cursorBlinkVisible
= blinkState
;
967 // We don't know how to dray glyphs to this screen, draw them as
968 // text and bail out.
969 putCharXY(x
, y
, cell
);
970 putCharXY(x
+ 1, y
, ' ', cell
);
974 if ((textWidth
!= lastTextWidth
) || (textHeight
!= lastTextHeight
)) {
975 // Screen size has changed, reset the font.
976 setupFont(textHeight
);
977 lastTextWidth
= textWidth
;
978 lastTextHeight
= textHeight
;
980 assert (doubleFont
!= null);
983 if (line
.getDoubleHeight() == 1) {
984 // Double-height top half: don't draw the underline.
985 Cell newCell
= new Cell(cell
);
986 newCell
.setUnderline(false);
987 image
= doubleFont
.getImage(newCell
, textWidth
* 2, textHeight
* 2,
990 image
= doubleFont
.getImage(cell
, textWidth
* 2, textHeight
* 2,
994 // Now that we have the double-wide glyph drawn, copy the right
995 // pieces of it to the cells.
996 Cell left
= new Cell(cell
);
997 Cell right
= new Cell(cell
);
999 BufferedImage leftImage
= null;
1000 BufferedImage rightImage
= null;
1002 System.err.println("image " + image + " textWidth " + textWidth +
1003 " textHeight " + textHeight);
1006 switch (line
.getDoubleHeight()) {
1008 // Top half double height
1009 leftImage
= image
.getSubimage(0, 0, textWidth
, textHeight
);
1010 rightImage
= image
.getSubimage(textWidth
, 0, textWidth
, textHeight
);
1013 // Bottom half double height
1014 leftImage
= image
.getSubimage(0, textHeight
, textWidth
, textHeight
);
1015 rightImage
= image
.getSubimage(textWidth
, textHeight
,
1016 textWidth
, textHeight
);
1019 // Either single height double-width, or error fallback
1020 BufferedImage wideImage
= new BufferedImage(textWidth
* 2,
1021 textHeight
, BufferedImage
.TYPE_INT_ARGB
);
1022 Graphics2D grWide
= wideImage
.createGraphics();
1023 grWide
.drawImage(image
, 0, 0, wideImage
.getWidth(),
1024 wideImage
.getHeight(), null);
1026 leftImage
= wideImage
.getSubimage(0, 0, textWidth
, textHeight
);
1027 rightImage
= wideImage
.getSubimage(textWidth
, 0, textWidth
,
1031 left
.setImage(leftImage
);
1032 right
.setImage(rightImage
);
1033 // Since we have image data, ditch the character here. Otherwise, a
1034 // drawBoxShadow() over the terminal window will show the characters
1035 // which looks wrong.
1038 putCharXY(x
, y
, left
);
1039 putCharXY(x
+ 1, y
, right
);
1043 * Set up the double-width font.
1045 * @param fontSize the size of font to request for the single-width font.
1046 * The double-width font will be 2x this value.
1048 private void setupFont(final int fontSize
) {
1049 doubleFont
= GlyphMaker
.getInstance(fontSize
* 2);
1051 // Special case: the ECMA48 backend needs to have a timer to drive
1053 if (getScreen() instanceof jexer
.backend
.ECMA48Terminal
) {
1055 // Blink every 500 millis.
1057 getApplication().addTimer(millis
, true,
1060 blinkState
= !blinkState
;
1061 getApplication().doRepaint();
1070 // ------------------------------------------------------------------------
1071 // DisplayListener --------------------------------------------------------
1072 // ------------------------------------------------------------------------
1075 * Called by emulator when fresh data has come in.
1077 public void displayChanged() {
1079 getApplication().postEvent(new TMenuEvent(TMenu
.MID_REPAINT
));
1083 * Function to call to obtain the display width.
1085 * @return the number of columns in the display
1087 public int getDisplayWidth() {
1095 * Function to call to obtain the display height.
1097 * @return the number of rows in the display
1099 public int getDisplayHeight() {