Commit | Line | Data |
---|---|---|
34a42e78 KL |
1 | /** |
2 | * Jexer - Java Text User Interface | |
3 | * | |
4 | * License: LGPLv3 or later | |
5 | * | |
6 | * This module is licensed under the GNU Lesser General Public License | |
7 | * Version 3. Please see the file "COPYING" in this directory for more | |
8 | * information about the GNU Lesser General Public License Version 3. | |
9 | * | |
10 | * Copyright (C) 2015 Kevin Lamonte | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public License | |
14 | * as published by the Free Software Foundation; either version 3 of | |
15 | * the License, or (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this program; if not, see | |
24 | * http://www.gnu.org/licenses/, or write to the Free Software | |
25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
26 | * 02110-1301 USA | |
27 | * | |
28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
29 | * @version 1 | |
30 | */ | |
31 | package jexer; | |
32 | ||
33 | import java.io.InputStream; | |
34 | import java.io.IOException; | |
35 | import java.io.OutputStream; | |
36 | import java.io.UnsupportedEncodingException; | |
37 | import java.util.LinkedList; | |
38 | import java.util.List; | |
39 | ||
40 | import jexer.bits.Cell; | |
41 | import jexer.bits.CellAttributes; | |
42 | import jexer.event.TKeypressEvent; | |
43 | import jexer.event.TMouseEvent; | |
44 | import jexer.event.TResizeEvent; | |
45 | import jexer.tterminal.DisplayLine; | |
46 | import jexer.tterminal.ECMA48; | |
47 | import static jexer.TKeypress.*; | |
48 | ||
49 | /** | |
50 | * TTerminalWindow exposes a ECMA-48 / ANSI X3.64 style terminal in a window. | |
51 | */ | |
52 | public class TTerminalWindow extends TWindow { | |
53 | ||
54 | /** | |
55 | * The emulator. | |
56 | */ | |
57 | private ECMA48 emulator; | |
58 | ||
59 | /** | |
60 | * The Process created by the shell spawning constructor. | |
61 | */ | |
62 | private Process shell; | |
63 | ||
64 | /** | |
65 | * Vertical scrollbar. | |
66 | */ | |
67 | private TVScroller vScroller; | |
68 | ||
69 | /** | |
70 | * Public constructor spawns a shell. | |
71 | * | |
72 | * @param application TApplication that manages this window | |
73 | * @param x column relative to parent | |
74 | * @param y row relative to parent | |
75 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
76 | */ | |
77 | public TTerminalWindow(final TApplication application, final int x, | |
78 | final int y, final int flags) { | |
79 | ||
80 | super(application, "Terminal", x, y, 80 + 2, 24 + 2, flags); | |
81 | ||
82 | try { | |
83 | String [] cmdShellWindows = { | |
84 | "cmd.exe" | |
85 | }; | |
86 | ||
87 | // You cannot run a login shell in a bare Process interactively, | |
88 | // due to libc's behavior of buffering when stdin/stdout aren't a | |
89 | // tty. Use 'script' instead to run a shell in a pty. | |
90 | String [] cmdShell = { | |
91 | "script", "-fqe", "/dev/null" | |
92 | }; | |
93 | // Spawn a shell and pass its I/O to the other constructor. | |
94 | ProcessBuilder pb; | |
95 | if (System.getProperty("os.name").startsWith("Windows")) { | |
96 | pb = new ProcessBuilder(cmdShellWindows); | |
97 | } else { | |
98 | pb = new ProcessBuilder(cmdShell); | |
99 | } | |
100 | // shell = Runtime.getRuntime().exec(cmdShell); | |
101 | ||
102 | // TODO: add LANG, TERM, LINES, and COLUMNS | |
103 | pb.redirectErrorStream(true); | |
104 | shell = pb.start(); | |
105 | emulator = new ECMA48(ECMA48.DeviceType.XTERM, | |
106 | shell.getInputStream(), | |
107 | shell.getOutputStream()); | |
108 | } catch (IOException e) { | |
109 | e.printStackTrace(); | |
110 | } | |
111 | ||
112 | // Setup the scroll bars | |
113 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(), | |
114 | getHeight())); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Public constructor. | |
119 | * | |
120 | * @param application TApplication that manages this window | |
121 | * @param x column relative to parent | |
122 | * @param y row relative to parent | |
123 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
124 | * @param input an InputStream connected to the remote side. For type == | |
125 | * XTERM, input is converted to a Reader with UTF-8 encoding. | |
126 | * @param output an OutputStream connected to the remote user. For type | |
127 | * == XTERM, output is converted to a Writer with UTF-8 encoding. | |
128 | * @throws UnsupportedEncodingException if an exception is thrown when | |
129 | * creating the InputStreamReader | |
130 | */ | |
131 | public TTerminalWindow(final TApplication application, final int x, | |
132 | final int y, final int flags, final InputStream input, | |
133 | final OutputStream output) throws UnsupportedEncodingException { | |
134 | ||
135 | super(application, "Terminal", x, y, 80 + 2, 24 + 2, flags); | |
136 | ||
137 | emulator = new ECMA48(ECMA48.DeviceType.XTERM, input, output); | |
138 | ||
139 | // Setup the scroll bars | |
140 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(), | |
141 | getHeight())); | |
142 | ||
143 | } | |
144 | ||
145 | /** | |
146 | * Draw the display buffer. | |
147 | */ | |
148 | @Override | |
149 | public void draw() { | |
150 | // Synchronize against the emulator so we don't stomp on its reader | |
151 | // thread. | |
152 | synchronized (emulator) { | |
153 | ||
154 | // Update the scroll bars | |
155 | reflow(); | |
156 | ||
157 | // Draw the box using my superclass | |
158 | super.draw(); | |
159 | ||
160 | List<DisplayLine> scrollback = emulator.getScrollbackBuffer(); | |
161 | List<DisplayLine> display = emulator.getDisplayBuffer(); | |
162 | ||
163 | // Put together the visible rows | |
164 | // System.err.printf("----------------------------\n"); | |
165 | // System.err.printf("vScroller.value %d\n", vScroller.getValue()); | |
166 | int visibleHeight = getHeight() - 2; | |
167 | // System.err.printf("visibleHeight %d\n", visibleHeight); | |
168 | int visibleBottom = scrollback.size() + display.size() | |
169 | + vScroller.getValue(); | |
170 | // System.err.printf("visibleBottom %d\n", visibleBottom); | |
171 | assert (visibleBottom >= 0); | |
172 | ||
173 | List<DisplayLine> preceedingBlankLines = new LinkedList<DisplayLine>(); | |
174 | int visibleTop = visibleBottom - visibleHeight; | |
175 | // System.err.printf("visibleTop %d\n", visibleTop); | |
176 | if (visibleTop < 0) { | |
177 | for (int i = visibleTop; i < 0; i++) { | |
178 | preceedingBlankLines.add(emulator.getBlankDisplayLine()); | |
179 | } | |
180 | visibleTop = 0; | |
181 | } | |
182 | assert (visibleTop >= 0); | |
183 | ||
184 | List<DisplayLine> displayLines = new LinkedList<DisplayLine>(); | |
185 | displayLines.addAll(scrollback); | |
186 | displayLines.addAll(display); | |
187 | // System.err.printf("displayLines.size %d\n", displayLines.size()); | |
188 | ||
189 | List<DisplayLine> visibleLines = new LinkedList<DisplayLine>(); | |
190 | visibleLines.addAll(preceedingBlankLines); | |
191 | visibleLines.addAll(displayLines.subList(visibleTop, | |
192 | visibleBottom)); | |
193 | // System.err.printf("visibleLines.size %d\n", visibleLines.size()); | |
194 | ||
195 | visibleHeight -= visibleLines.size(); | |
196 | // System.err.printf("visibleHeight %d\n", visibleHeight); | |
197 | assert (visibleHeight >= 0); | |
198 | ||
199 | // Now draw the emulator screen | |
200 | int row = 1; | |
201 | for (DisplayLine line: visibleLines) { | |
202 | int widthMax = emulator.getWidth(); | |
203 | if (line.isDoubleWidth()) { | |
204 | widthMax /= 2; | |
205 | } | |
206 | if (widthMax > getWidth() - 2) { | |
207 | widthMax = getWidth() - 2; | |
208 | } | |
209 | for (int i = 0; i < widthMax; i++) { | |
210 | Cell ch = line.charAt(i); | |
211 | Cell newCell = new Cell(); | |
212 | newCell.setTo(ch); | |
213 | boolean reverse = line.isReverseColor() ^ ch.getReverse(); | |
214 | newCell.setReverse(false); | |
215 | if (reverse) { | |
216 | newCell.setBackColor(ch.getForeColor()); | |
217 | newCell.setForeColor(ch.getBackColor()); | |
218 | } | |
219 | if (line.isDoubleWidth()) { | |
220 | getScreen().putCharXY((i * 2) + 1, row, newCell); | |
221 | getScreen().putCharXY((i * 2) + 2, row, ' ', newCell); | |
222 | } else { | |
223 | getScreen().putCharXY(i + 1, row, newCell); | |
224 | } | |
225 | } | |
226 | row++; | |
227 | if (row == getHeight() - 1) { | |
228 | // Don't overwrite the box edge | |
229 | break; | |
230 | } | |
231 | } | |
232 | CellAttributes background = new CellAttributes(); | |
233 | // Fill in the blank lines on bottom | |
234 | for (int i = 0; i < visibleHeight; i++) { | |
235 | getScreen().hLineXY(1, i + row, getWidth() - 2, ' ', | |
236 | background); | |
237 | } | |
238 | ||
239 | } // synchronized (emulator) | |
240 | ||
241 | } | |
242 | ||
243 | /** | |
244 | * Handle window close. | |
245 | */ | |
246 | @Override public void onClose() { | |
247 | emulator.close(); | |
248 | } | |
249 | ||
250 | /** | |
251 | * Copy out variables from the emulator that TTerminal has to expose on | |
252 | * screen. | |
253 | */ | |
254 | private void readEmulatorState() { | |
255 | // Synchronize against the emulator so we don't stomp on its reader | |
256 | // thread. | |
257 | synchronized (emulator) { | |
258 | ||
259 | setCursorX(emulator.getCursorX() + 1); | |
260 | setCursorY(emulator.getCursorY() + 1 | |
261 | + (getHeight() - 2 - emulator.getHeight())); | |
262 | if (vScroller != null) { | |
263 | setCursorY(getCursorY() - vScroller.getValue()); | |
264 | } | |
265 | setHasCursor(emulator.visibleCursor()); | |
266 | if (getCursorX() > getWidth() - 2) { | |
267 | setHasCursor(false); | |
268 | } | |
269 | if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) { | |
270 | setHasCursor(false); | |
271 | } | |
272 | if (emulator.getScreenTitle().length() > 0) { | |
273 | // Only update the title if the shell is still alive | |
274 | if (shell != null) { | |
275 | setTitle(emulator.getScreenTitle()); | |
276 | } | |
277 | } | |
278 | setMaximumWindowWidth(emulator.getWidth() + 2); | |
279 | ||
280 | // Check to see if the shell has died. | |
281 | if (!emulator.isReading() && (shell != null)) { | |
282 | // The emulator exited on its own, all is fine | |
283 | setTitle(String.format("%s [Completed - %d]", | |
284 | getTitle(), shell.exitValue())); | |
285 | shell = null; | |
286 | emulator.close(); | |
287 | } else if (emulator.isReading() && (shell != null)) { | |
288 | // The shell might be dead, let's check | |
289 | try { | |
290 | int rc = shell.exitValue(); | |
291 | // If we got here, the shell died. | |
292 | setTitle(String.format("%s [Completed - %d]", | |
293 | getTitle(), rc)); | |
294 | shell = null; | |
295 | emulator.close(); | |
296 | } catch (IllegalThreadStateException e) { | |
297 | // The shell is still running, do nothing. | |
298 | } | |
299 | } | |
300 | ||
301 | } // synchronized (emulator) | |
302 | } | |
303 | ||
304 | /** | |
305 | * Handle window/screen resize events. | |
306 | * | |
307 | * @param resize resize event | |
308 | */ | |
309 | @Override | |
310 | public void onResize(final TResizeEvent resize) { | |
311 | ||
312 | // Synchronize against the emulator so we don't stomp on its reader | |
313 | // thread. | |
314 | synchronized (emulator) { | |
315 | ||
316 | if (resize.getType() == TResizeEvent.Type.WIDGET) { | |
317 | // Resize the scroll bars | |
318 | reflow(); | |
319 | ||
320 | // Get out of scrollback | |
321 | vScroller.setValue(0); | |
322 | } | |
323 | return; | |
324 | ||
325 | } // synchronized (emulator) | |
326 | } | |
327 | ||
328 | /** | |
329 | * Resize scrollbars for a new width/height. | |
330 | */ | |
331 | private void reflow() { | |
332 | ||
333 | // Synchronize against the emulator so we don't stomp on its reader | |
334 | // thread. | |
335 | synchronized (emulator) { | |
336 | ||
337 | // Pull cursor information | |
338 | readEmulatorState(); | |
339 | ||
340 | // Vertical scrollbar | |
341 | if (vScroller == null) { | |
342 | vScroller = new TVScroller(this, getWidth() - 2, 0, | |
343 | getHeight() - 2); | |
344 | vScroller.setBottomValue(0); | |
345 | vScroller.setValue(0); | |
346 | } else { | |
347 | vScroller.setX(getWidth() - 2); | |
348 | vScroller.setHeight(getHeight() - 2); | |
349 | } | |
350 | vScroller.setTopValue(getHeight() - 2 | |
351 | - (emulator.getScrollbackBuffer().size() | |
352 | + emulator.getDisplayBuffer().size())); | |
353 | vScroller.setBigChange(getHeight() - 2); | |
354 | ||
355 | } // synchronized (emulator) | |
356 | } | |
357 | ||
358 | /** | |
359 | * Handle keystrokes. | |
360 | * | |
361 | * @param keypress keystroke event | |
362 | */ | |
363 | @Override | |
364 | public void onKeypress(final TKeypressEvent keypress) { | |
365 | ||
366 | // Scrollback up/down | |
367 | if (keypress.equals(kbShiftPgUp) | |
368 | || keypress.equals(kbCtrlPgUp) | |
369 | || keypress.equals(kbAltPgUp) | |
370 | ) { | |
371 | vScroller.bigDecrement(); | |
372 | return; | |
373 | } | |
374 | if (keypress.equals(kbShiftPgDn) | |
375 | || keypress.equals(kbCtrlPgDn) | |
376 | || keypress.equals(kbAltPgDn) | |
377 | ) { | |
378 | vScroller.bigIncrement(); | |
379 | return; | |
380 | } | |
381 | ||
382 | // Synchronize against the emulator so we don't stomp on its reader | |
383 | // thread. | |
384 | synchronized (emulator) { | |
385 | if (emulator.isReading()) { | |
386 | // Get out of scrollback | |
387 | vScroller.setValue(0); | |
388 | emulator.keypress(keypress.getKey()); | |
389 | readEmulatorState(); | |
390 | return; | |
391 | } | |
392 | } | |
393 | ||
394 | // Process is closed, honor "normal" TUI keystrokes | |
395 | super.onKeypress(keypress); | |
396 | } | |
397 | ||
398 | /** | |
399 | * Handle mouse press events. | |
400 | * | |
401 | * @param mouse mouse button press event | |
402 | */ | |
403 | @Override | |
404 | public void onMouseDown(final TMouseEvent mouse) { | |
405 | ||
406 | if (mouse.getMouseWheelUp()) { | |
407 | vScroller.decrement(); | |
408 | return; | |
409 | } | |
410 | if (mouse.getMouseWheelDown()) { | |
411 | vScroller.increment(); | |
412 | return; | |
413 | } | |
414 | ||
415 | // Pass to children | |
416 | super.onMouseDown(mouse); | |
417 | } | |
418 | ||
419 | } |