2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2016 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]
31 import java
.io
.InputStream
;
32 import java
.io
.IOException
;
33 import java
.io
.OutputStream
;
34 import java
.io
.UnsupportedEncodingException
;
35 import java
.util
.LinkedList
;
36 import java
.util
.List
;
39 import jexer
.bits
.Cell
;
40 import jexer
.bits
.CellAttributes
;
41 import jexer
.event
.TKeypressEvent
;
42 import jexer
.event
.TMouseEvent
;
43 import jexer
.event
.TResizeEvent
;
44 import jexer
.tterminal
.DisplayLine
;
45 import jexer
.tterminal
.ECMA48
;
46 import static jexer
.TKeypress
.*;
49 * TTerminalWindow exposes a ECMA-48 / ANSI X3.64 style terminal in a window.
51 public class TTerminalWindow
extends TWindow
{
56 private ECMA48 emulator
;
59 * The Process created by the shell spawning constructor.
61 private Process shell
;
66 private TVScroller vScroller
;
69 * Claim the keystrokes the emulator will need.
71 private void addShortcutKeys() {
72 addShortcutKeypress(kbCtrlA
);
73 addShortcutKeypress(kbCtrlB
);
74 addShortcutKeypress(kbCtrlC
);
75 addShortcutKeypress(kbCtrlD
);
76 addShortcutKeypress(kbCtrlE
);
77 addShortcutKeypress(kbCtrlF
);
78 addShortcutKeypress(kbCtrlG
);
79 addShortcutKeypress(kbCtrlH
);
80 addShortcutKeypress(kbCtrlU
);
81 addShortcutKeypress(kbCtrlJ
);
82 addShortcutKeypress(kbCtrlK
);
83 addShortcutKeypress(kbCtrlL
);
84 addShortcutKeypress(kbCtrlM
);
85 addShortcutKeypress(kbCtrlN
);
86 addShortcutKeypress(kbCtrlO
);
87 addShortcutKeypress(kbCtrlP
);
88 addShortcutKeypress(kbCtrlQ
);
89 addShortcutKeypress(kbCtrlR
);
90 addShortcutKeypress(kbCtrlS
);
91 addShortcutKeypress(kbCtrlT
);
92 addShortcutKeypress(kbCtrlU
);
93 addShortcutKeypress(kbCtrlV
);
94 addShortcutKeypress(kbCtrlW
);
95 addShortcutKeypress(kbCtrlX
);
96 addShortcutKeypress(kbCtrlY
);
97 addShortcutKeypress(kbCtrlZ
);
98 addShortcutKeypress(kbF1
);
99 addShortcutKeypress(kbF2
);
100 addShortcutKeypress(kbF3
);
101 addShortcutKeypress(kbF4
);
102 addShortcutKeypress(kbF5
);
103 addShortcutKeypress(kbF6
);
104 addShortcutKeypress(kbF7
);
105 addShortcutKeypress(kbF8
);
106 addShortcutKeypress(kbF9
);
107 addShortcutKeypress(kbF10
);
108 addShortcutKeypress(kbF11
);
109 addShortcutKeypress(kbF12
);
110 addShortcutKeypress(kbAltA
);
111 addShortcutKeypress(kbAltB
);
112 addShortcutKeypress(kbAltC
);
113 addShortcutKeypress(kbAltD
);
114 addShortcutKeypress(kbAltE
);
115 addShortcutKeypress(kbAltF
);
116 addShortcutKeypress(kbAltG
);
117 addShortcutKeypress(kbAltH
);
118 addShortcutKeypress(kbAltU
);
119 addShortcutKeypress(kbAltJ
);
120 addShortcutKeypress(kbAltK
);
121 addShortcutKeypress(kbAltL
);
122 addShortcutKeypress(kbAltM
);
123 addShortcutKeypress(kbAltN
);
124 addShortcutKeypress(kbAltO
);
125 addShortcutKeypress(kbAltP
);
126 addShortcutKeypress(kbAltQ
);
127 addShortcutKeypress(kbAltR
);
128 addShortcutKeypress(kbAltS
);
129 addShortcutKeypress(kbAltT
);
130 addShortcutKeypress(kbAltU
);
131 addShortcutKeypress(kbAltV
);
132 addShortcutKeypress(kbAltW
);
133 addShortcutKeypress(kbAltX
);
134 addShortcutKeypress(kbAltY
);
135 addShortcutKeypress(kbAltZ
);
139 * Public constructor spawns a shell.
141 * @param application TApplication that manages this window
142 * @param x column relative to parent
143 * @param y row relative to parent
144 * @param flags mask of CENTERED, MODAL, or RESIZABLE
146 public TTerminalWindow(final TApplication application
, final int x
,
147 final int y
, final int flags
) {
149 super(application
, "Terminal", x
, y
, 80 + 2, 24 + 2, flags
);
152 ECMA48
.DeviceType deviceType
= ECMA48
.DeviceType
.XTERM
;
155 String
[] cmdShellWindows
= {
159 // You cannot run a login shell in a bare Process interactively,
160 // due to libc's behavior of buffering when stdin/stdout aren't a
161 // tty. Use 'script' instead to run a shell in a pty.
162 String
[] cmdShell
= {
163 "script", "-fqe", "/dev/null"
165 // Spawn a shell and pass its I/O to the other constructor.
168 if (System
.getProperty("os.name").startsWith("Windows")) {
169 pb
= new ProcessBuilder(cmdShellWindows
);
171 pb
= new ProcessBuilder(cmdShell
);
173 Map
<String
, String
> env
= pb
.environment();
174 env
.put("TERM", ECMA48
.deviceTypeTerm(deviceType
));
175 env
.put("LANG", ECMA48
.deviceTypeLang(deviceType
, "en"));
176 env
.put("COLUMNS", "80");
177 env
.put("LINES", "24");
178 pb
.redirectErrorStream(true);
180 emulator
= new ECMA48(deviceType
, shell
.getInputStream(),
181 shell
.getOutputStream());
182 } catch (IOException e
) {
186 // Setup the scroll bars
187 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
190 // Claim the keystrokes the emulator will need.
195 * Public constructor.
197 * @param application TApplication that manages this window
198 * @param x column relative to parent
199 * @param y row relative to parent
200 * @param flags mask of CENTERED, MODAL, or RESIZABLE
201 * @param input an InputStream connected to the remote side. For type ==
202 * XTERM, input is converted to a Reader with UTF-8 encoding.
203 * @param output an OutputStream connected to the remote user. For type
204 * == XTERM, output is converted to a Writer with UTF-8 encoding.
205 * @throws UnsupportedEncodingException if an exception is thrown when
206 * creating the InputStreamReader
208 public TTerminalWindow(final TApplication application
, final int x
,
209 final int y
, final int flags
, final InputStream input
,
210 final OutputStream output
) throws UnsupportedEncodingException
{
212 super(application
, "Terminal", x
, y
, 80 + 2, 24 + 2, flags
);
214 emulator
= new ECMA48(ECMA48
.DeviceType
.XTERM
, input
, output
);
216 // Setup the scroll bars
217 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
220 // Claim the keystrokes the emulator will need.
225 * Draw the display buffer.
229 // Synchronize against the emulator so we don't stomp on its reader
231 synchronized (emulator
) {
233 // Update the scroll bars
236 // Draw the box using my superclass
239 List
<DisplayLine
> scrollback
= emulator
.getScrollbackBuffer();
240 List
<DisplayLine
> display
= emulator
.getDisplayBuffer();
242 // Put together the visible rows
243 int visibleHeight
= getHeight() - 2;
244 int visibleBottom
= scrollback
.size() + display
.size()
245 + vScroller
.getValue();
246 assert (visibleBottom
>= 0);
248 List
<DisplayLine
> preceedingBlankLines
= new LinkedList
<DisplayLine
>();
249 int visibleTop
= visibleBottom
- visibleHeight
;
250 if (visibleTop
< 0) {
251 for (int i
= visibleTop
; i
< 0; i
++) {
252 preceedingBlankLines
.add(emulator
.getBlankDisplayLine());
256 assert (visibleTop
>= 0);
258 List
<DisplayLine
> displayLines
= new LinkedList
<DisplayLine
>();
259 displayLines
.addAll(scrollback
);
260 displayLines
.addAll(display
);
262 List
<DisplayLine
> visibleLines
= new LinkedList
<DisplayLine
>();
263 visibleLines
.addAll(preceedingBlankLines
);
264 visibleLines
.addAll(displayLines
.subList(visibleTop
,
267 visibleHeight
-= visibleLines
.size();
268 assert (visibleHeight
>= 0);
270 // Now draw the emulator screen
272 for (DisplayLine line
: visibleLines
) {
273 int widthMax
= emulator
.getWidth();
274 if (line
.isDoubleWidth()) {
277 if (widthMax
> getWidth() - 2) {
278 widthMax
= getWidth() - 2;
280 for (int i
= 0; i
< widthMax
; i
++) {
281 Cell ch
= line
.charAt(i
);
282 Cell newCell
= new Cell();
284 boolean reverse
= line
.isReverseColor() ^ ch
.isReverse();
285 newCell
.setReverse(false);
287 newCell
.setBackColor(ch
.getForeColor());
288 newCell
.setForeColor(ch
.getBackColor());
290 if (line
.isDoubleWidth()) {
291 getScreen().putCharXY((i
* 2) + 1, row
, newCell
);
292 getScreen().putCharXY((i
* 2) + 2, row
, ' ', newCell
);
294 getScreen().putCharXY(i
+ 1, row
, newCell
);
298 if (row
== getHeight() - 1) {
299 // Don't overwrite the box edge
303 CellAttributes background
= new CellAttributes();
304 // Fill in the blank lines on bottom
305 for (int i
= 0; i
< visibleHeight
; i
++) {
306 getScreen().hLineXY(1, i
+ row
, getWidth() - 2, ' ',
310 } // synchronized (emulator)
315 * Handle window close.
317 @Override public void onClose() {
320 // System.err.println("shell.destroy()");
327 * Copy out variables from the emulator that TTerminal has to expose on
330 private void readEmulatorState() {
331 // Synchronize against the emulator so we don't stomp on its reader
333 synchronized (emulator
) {
335 setCursorX(emulator
.getCursorX() + 1);
336 setCursorY(emulator
.getCursorY() + 1
337 + (getHeight() - 2 - emulator
.getHeight()));
338 if (vScroller
!= null) {
339 setCursorY(getCursorY() - vScroller
.getValue());
341 setCursorVisible(emulator
.isCursorVisible());
342 if (getCursorX() > getWidth() - 2) {
343 setCursorVisible(false);
345 if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
346 setCursorVisible(false);
348 if (emulator
.getScreenTitle().length() > 0) {
349 // Only update the title if the shell is still alive
351 setTitle(emulator
.getScreenTitle());
355 // Check to see if the shell has died.
356 if (!emulator
.isReading() && (shell
!= null)) {
358 int rc
= shell
.exitValue();
359 // The emulator exited on its own, all is fine
360 setTitle(String
.format("%s [Completed - %d]",
364 clearShortcutKeypresses();
365 } catch (IllegalThreadStateException e
) {
366 // The emulator thread has exited, but the shell Process
367 // hasn't figured that out yet. Do nothing, we will see
368 // this in a future tick.
370 } else if (emulator
.isReading() && (shell
!= null)) {
371 // The shell might be dead, let's check
373 int rc
= shell
.exitValue();
374 // If we got here, the shell died.
375 setTitle(String
.format("%s [Completed - %d]",
379 clearShortcutKeypresses();
380 } catch (IllegalThreadStateException e
) {
381 // The shell is still running, do nothing.
385 } // synchronized (emulator)
389 * Handle window/screen resize events.
391 * @param resize resize event
394 public void onResize(final TResizeEvent resize
) {
396 // Synchronize against the emulator so we don't stomp on its reader
398 synchronized (emulator
) {
400 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
401 // Resize the scroll bars
404 // Get out of scrollback
405 vScroller
.setValue(0);
409 } // synchronized (emulator)
413 * Resize scrollbars for a new width/height.
415 private void reflow() {
417 // Synchronize against the emulator so we don't stomp on its reader
419 synchronized (emulator
) {
421 // Pull cursor information
424 // Vertical scrollbar
425 if (vScroller
== null) {
426 vScroller
= new TVScroller(this, getWidth() - 2, 0,
428 vScroller
.setBottomValue(0);
429 vScroller
.setValue(0);
431 vScroller
.setX(getWidth() - 2);
432 vScroller
.setHeight(getHeight() - 2);
434 vScroller
.setTopValue(getHeight() - 2
435 - (emulator
.getScrollbackBuffer().size()
436 + emulator
.getDisplayBuffer().size()));
437 vScroller
.setBigChange(getHeight() - 2);
439 } // synchronized (emulator)
443 * Check if a mouse press/release/motion event coordinate is over the
446 * @param mouse a mouse-based event
447 * @return whether or not the mouse is on the emulator
449 private final boolean mouseOnEmulator(final TMouseEvent mouse
) {
451 synchronized (emulator
) {
452 if (!emulator
.isReading()) {
457 if ((mouse
.getAbsoluteX() >= getAbsoluteX() + 1)
458 && (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
459 && (mouse
.getAbsoluteY() >= getAbsoluteY() + 1)
460 && (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
470 * @param keypress keystroke event
473 public void onKeypress(final TKeypressEvent keypress
) {
475 // Scrollback up/down
476 if (keypress
.equals(kbShiftPgUp
)
477 || keypress
.equals(kbCtrlPgUp
)
478 || keypress
.equals(kbAltPgUp
)
480 vScroller
.bigDecrement();
483 if (keypress
.equals(kbShiftPgDn
)
484 || keypress
.equals(kbCtrlPgDn
)
485 || keypress
.equals(kbAltPgDn
)
487 vScroller
.bigIncrement();
491 // Synchronize against the emulator so we don't stomp on its reader
493 synchronized (emulator
) {
494 if (emulator
.isReading()) {
495 // Get out of scrollback
496 vScroller
.setValue(0);
497 emulator
.keypress(keypress
.getKey());
499 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
500 // this is kBEnter then also send kbCtrlJ.
501 if (System
.getProperty("os.name").startsWith("Windows")) {
502 if (keypress
.equals(kbEnter
)) {
503 emulator
.keypress(kbCtrlJ
);
512 // Process is closed, honor "normal" TUI keystrokes
513 super.onKeypress(keypress
);
517 * Handle mouse press events.
519 * @param mouse mouse button press event
522 public void onMouseDown(final TMouseEvent mouse
) {
523 if (inWindowMove
|| inWindowResize
) {
524 // TWindow needs to deal with this.
525 super.onMouseDown(mouse
);
529 if (mouse
.isMouseWheelUp()) {
530 vScroller
.decrement();
533 if (mouse
.isMouseWheelDown()) {
534 vScroller
.increment();
537 if (mouseOnEmulator(mouse
)) {
538 synchronized (emulator
) {
539 mouse
.setX(mouse
.getX() - 1);
540 mouse
.setY(mouse
.getY() - 1);
541 emulator
.mouse(mouse
);
547 // Emulator didn't consume it, pass it on
548 super.onMouseDown(mouse
);
552 * Handle mouse release events.
554 * @param mouse mouse button release event
557 public void onMouseUp(final TMouseEvent mouse
) {
558 if (inWindowMove
|| inWindowResize
) {
559 // TWindow needs to deal with this.
560 super.onMouseUp(mouse
);
564 if (mouseOnEmulator(mouse
)) {
565 synchronized (emulator
) {
566 mouse
.setX(mouse
.getX() - 1);
567 mouse
.setY(mouse
.getY() - 1);
568 emulator
.mouse(mouse
);
574 // Emulator didn't consume it, pass it on
575 super.onMouseUp(mouse
);
579 * Handle mouse motion events.
581 * @param mouse mouse motion event
584 public void onMouseMotion(final TMouseEvent mouse
) {
585 if (inWindowMove
|| inWindowResize
) {
586 // TWindow needs to deal with this.
587 super.onMouseMotion(mouse
);
591 if (mouseOnEmulator(mouse
)) {
592 synchronized (emulator
) {
593 mouse
.setX(mouse
.getX() - 1);
594 mouse
.setY(mouse
.getY() - 1);
595 emulator
.mouse(mouse
);
601 // Emulator didn't consume it, pass it on
602 super.onMouseMotion(mouse
);