Commit | Line | Data |
---|---|---|
4d2c61b4 KL |
1 | /* |
2 | * Jexer - Java Text User Interface | |
3 | * | |
4 | * The MIT License (MIT) | |
5 | * | |
6 | * Copyright (C) 2019 Kevin Lamonte | |
7 | * | |
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: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
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. | |
25 | * | |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
31 | import java.awt.Font; | |
32 | import java.awt.FontMetrics; | |
33 | import java.awt.Graphics2D; | |
34 | import java.awt.image.BufferedImage; | |
35 | ||
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; | |
43 | import java.util.Map; | |
44 | import java.util.ResourceBundle; | |
45 | ||
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.*; | |
61 | ||
62 | /** | |
2bc32111 | 63 | * TTerminalWidget exposes a ECMA-48 / ANSI X3.64 style terminal in a widget. |
4d2c61b4 KL |
64 | */ |
65 | public class TTerminalWidget extends TScrollableWidget | |
66 | implements DisplayListener { | |
67 | ||
68 | /** | |
69 | * Translated strings. | |
70 | */ | |
71 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TTerminalWidget.class.getName()); | |
72 | ||
73 | // ------------------------------------------------------------------------ | |
74 | // Variables -------------------------------------------------------------- | |
75 | // ------------------------------------------------------------------------ | |
76 | ||
77 | /** | |
78 | * The emulator. | |
79 | */ | |
80 | private ECMA48 emulator; | |
81 | ||
82 | /** | |
83 | * The Process created by the shell spawning constructor. | |
84 | */ | |
85 | private Process shell; | |
86 | ||
87 | /** | |
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 . | |
91 | */ | |
92 | private boolean ptypipe = false; | |
93 | ||
94 | /** | |
95 | * Double-height font. | |
96 | */ | |
97 | private GlyphMaker doubleFont; | |
98 | ||
99 | /** | |
100 | * Last text width value. | |
101 | */ | |
102 | private int lastTextWidth = -1; | |
103 | ||
104 | /** | |
105 | * Last text height value. | |
106 | */ | |
107 | private int lastTextHeight = -1; | |
108 | ||
109 | /** | |
110 | * The blink state, used only by ECMA48 backend and when double-width | |
111 | * chars must be drawn. | |
112 | */ | |
113 | private boolean blinkState = true; | |
114 | ||
115 | /** | |
116 | * Timer flag, used only by ECMA48 backend and when double-width chars | |
117 | * must be drawn. | |
118 | */ | |
119 | private boolean haveTimer = false; | |
120 | ||
121 | /** | |
9917c620 | 122 | * The last seen visible display. |
4d2c61b4 KL |
123 | */ |
124 | private List<DisplayLine> display; | |
125 | ||
126 | /** | |
127 | * If true, the display has changed and needs updating. | |
128 | */ | |
129 | private volatile boolean dirty = true; | |
130 | ||
131 | /** | |
132 | * Time that the display was last updated. | |
133 | */ | |
134 | private long lastUpdateTime = 0; | |
135 | ||
136 | /** | |
137 | * If true, hide the mouse after typing a keystroke. | |
138 | */ | |
139 | private boolean hideMouseWhenTyping = true; | |
140 | ||
141 | /** | |
142 | * If true, the mouse should not be displayed because a keystroke was | |
143 | * typed. | |
144 | */ | |
145 | private boolean typingHidMouse = false; | |
146 | ||
147 | /** | |
148 | * The return value from the emulator. | |
149 | */ | |
150 | private int exitValue = -1; | |
151 | ||
152 | /** | |
153 | * Title to expose to a window. | |
154 | */ | |
155 | private String title = ""; | |
156 | ||
2bc32111 KL |
157 | /** |
158 | * Action to perform when the terminal exits. | |
159 | */ | |
160 | private TAction closeAction = null; | |
161 | ||
4d2c61b4 KL |
162 | // ------------------------------------------------------------------------ |
163 | // Constructors ----------------------------------------------------------- | |
164 | // ------------------------------------------------------------------------ | |
165 | ||
166 | /** | |
167 | * Public constructor spawns a custom command line. | |
168 | * | |
169 | * @param parent parent widget | |
170 | * @param x column relative to parent | |
171 | * @param y row relative to parent | |
172 | * @param commandLine the command line to execute | |
173 | */ | |
2bc32111 KL |
174 | public TTerminalWidget(final TWidget parent, final int x, final int y, |
175 | final String commandLine) { | |
4d2c61b4 KL |
176 | |
177 | this(parent, x, y, commandLine.split("\\s+")); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Public constructor spawns a custom command line. | |
182 | * | |
183 | * @param parent parent widget | |
184 | * @param x column relative to parent | |
185 | * @param y row relative to parent | |
186 | * @param command the command line to execute | |
187 | */ | |
2bc32111 KL |
188 | public TTerminalWidget(final TWidget parent, final int x, final int y, |
189 | final String [] command) { | |
190 | ||
191 | this(parent, x, y, command, null); | |
192 | } | |
193 | ||
194 | /** | |
195 | * Public constructor spawns a custom command line. | |
196 | * | |
197 | * @param parent parent widget | |
198 | * @param x column relative to parent | |
199 | * @param y row relative to parent | |
200 | * @param command the command line to execute | |
201 | * @param closeAction action to perform when the shell sxits | |
202 | */ | |
203 | public TTerminalWidget(final TWidget parent, final int x, final int y, | |
204 | final String [] command, final TAction closeAction) { | |
205 | ||
206 | this(parent, x, y, 80, 24, command, closeAction); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Public constructor spawns a custom command line. | |
211 | * | |
212 | * @param parent parent widget | |
213 | * @param x column relative to parent | |
214 | * @param y row relative to parent | |
215 | * @param width width of widget | |
216 | * @param height height of widget | |
217 | * @param command the command line to execute | |
218 | * @param closeAction action to perform when the shell sxits | |
219 | */ | |
220 | public TTerminalWidget(final TWidget parent, final int x, final int y, | |
221 | final int width, final int height, final String [] command, | |
222 | final TAction closeAction) { | |
223 | ||
224 | super(parent, x, y, width, height); | |
4d2c61b4 | 225 | |
2bc32111 | 226 | this.closeAction = closeAction; |
4d2c61b4 KL |
227 | |
228 | String [] fullCommand; | |
229 | ||
230 | // Spawn a shell and pass its I/O to the other constructor. | |
231 | if ((System.getProperty("jexer.TTerminal.ptypipe") != null) | |
232 | && (System.getProperty("jexer.TTerminal.ptypipe"). | |
233 | equals("true")) | |
234 | ) { | |
235 | ptypipe = true; | |
236 | fullCommand = new String[command.length + 1]; | |
237 | fullCommand[0] = "ptypipe"; | |
238 | System.arraycopy(command, 0, fullCommand, 1, command.length); | |
239 | } else if (System.getProperty("os.name").startsWith("Windows")) { | |
240 | fullCommand = new String[3]; | |
241 | fullCommand[0] = "cmd"; | |
242 | fullCommand[1] = "/c"; | |
243 | fullCommand[2] = stringArrayToString(command); | |
244 | } else if (System.getProperty("os.name").startsWith("Mac")) { | |
245 | fullCommand = new String[6]; | |
246 | fullCommand[0] = "script"; | |
247 | fullCommand[1] = "-q"; | |
248 | fullCommand[2] = "-F"; | |
249 | fullCommand[3] = "/dev/null"; | |
250 | fullCommand[4] = "-c"; | |
251 | fullCommand[5] = stringArrayToString(command); | |
252 | } else { | |
253 | // Default: behave like Linux | |
254 | fullCommand = new String[5]; | |
255 | fullCommand[0] = "script"; | |
256 | fullCommand[1] = "-fqe"; | |
257 | fullCommand[2] = "/dev/null"; | |
258 | fullCommand[3] = "-c"; | |
259 | fullCommand[4] = stringArrayToString(command); | |
260 | } | |
261 | spawnShell(fullCommand); | |
262 | } | |
263 | ||
264 | /** | |
265 | * Public constructor spawns a shell. | |
266 | * | |
267 | * @param parent parent widget | |
268 | * @param x column relative to parent | |
269 | * @param y row relative to parent | |
270 | */ | |
271 | public TTerminalWidget(final TWidget parent, final int x, final int y) { | |
2bc32111 KL |
272 | this(parent, x, y, (TAction) null); |
273 | } | |
4d2c61b4 | 274 | |
2bc32111 KL |
275 | /** |
276 | * Public constructor spawns a shell. | |
277 | * | |
278 | * @param parent parent widget | |
279 | * @param x column relative to parent | |
280 | * @param y row relative to parent | |
281 | * @param closeAction action to perform when the shell sxits | |
282 | */ | |
283 | public TTerminalWidget(final TWidget parent, final int x, final int y, | |
284 | final TAction closeAction) { | |
285 | ||
286 | this(parent, x, y, 80, 24, closeAction); | |
287 | } | |
288 | ||
289 | /** | |
290 | * Public constructor spawns a shell. | |
291 | * | |
292 | * @param parent parent widget | |
293 | * @param x column relative to parent | |
294 | * @param y row relative to parent | |
295 | * @param width width of widget | |
296 | * @param height height of widget | |
297 | * @param closeAction action to perform when the shell sxits | |
298 | */ | |
299 | public TTerminalWidget(final TWidget parent, final int x, final int y, | |
300 | final int width, final int height, final TAction closeAction) { | |
301 | ||
302 | super(parent, x, y, width, height); | |
303 | ||
304 | this.closeAction = closeAction; | |
4d2c61b4 KL |
305 | |
306 | if (System.getProperty("jexer.TTerminal.shell") != null) { | |
307 | String shell = System.getProperty("jexer.TTerminal.shell"); | |
308 | if (shell.trim().startsWith("ptypipe")) { | |
309 | ptypipe = true; | |
310 | } | |
311 | spawnShell(shell.split("\\s+")); | |
312 | return; | |
313 | } | |
314 | ||
315 | String cmdShellWindows = "cmd.exe"; | |
316 | ||
317 | // You cannot run a login shell in a bare Process interactively, due | |
318 | // to libc's behavior of buffering when stdin/stdout aren't a tty. | |
319 | // Use 'script' instead to run a shell in a pty. And because BSD and | |
320 | // GNU differ on the '-f' vs '-F' flags, we need two different | |
321 | // commands. Lovely. | |
322 | String cmdShellGNU = "script -fqe /dev/null"; | |
323 | String cmdShellBSD = "script -q -F /dev/null"; | |
324 | ||
325 | // ptypipe is another solution that permits dynamic window resizing. | |
326 | String cmdShellPtypipe = "ptypipe /bin/bash --login"; | |
327 | ||
328 | // Spawn a shell and pass its I/O to the other constructor. | |
329 | if ((System.getProperty("jexer.TTerminal.ptypipe") != null) | |
330 | && (System.getProperty("jexer.TTerminal.ptypipe"). | |
331 | equals("true")) | |
332 | ) { | |
333 | ptypipe = true; | |
334 | spawnShell(cmdShellPtypipe.split("\\s+")); | |
335 | } else if (System.getProperty("os.name").startsWith("Windows")) { | |
336 | spawnShell(cmdShellWindows.split("\\s+")); | |
337 | } else if (System.getProperty("os.name").startsWith("Mac")) { | |
338 | spawnShell(cmdShellBSD.split("\\s+")); | |
339 | } else if (System.getProperty("os.name").startsWith("Linux")) { | |
340 | spawnShell(cmdShellGNU.split("\\s+")); | |
341 | } else { | |
342 | // When all else fails, assume GNU. | |
343 | spawnShell(cmdShellGNU.split("\\s+")); | |
344 | } | |
345 | } | |
346 | ||
347 | // ------------------------------------------------------------------------ | |
348 | // TScrollableWidget ------------------------------------------------------ | |
349 | // ------------------------------------------------------------------------ | |
350 | ||
351 | /** | |
352 | * Draw the display buffer. | |
353 | */ | |
354 | @Override | |
355 | public void draw() { | |
356 | int width = getDisplayWidth(); | |
2bc32111 | 357 | |
4d2c61b4 | 358 | boolean syncEmulator = false; |
9917c620 | 359 | if ((System.currentTimeMillis() - lastUpdateTime >= 20) |
4d2c61b4 KL |
360 | && (dirty == true) |
361 | ) { | |
362 | // Too much time has passed, draw it all. | |
363 | syncEmulator = true; | |
364 | } else if (emulator.isReading() && (dirty == false)) { | |
365 | // Wait until the emulator has brought more data in. | |
366 | syncEmulator = false; | |
367 | } else if (!emulator.isReading() && (dirty == true)) { | |
368 | // The emulator won't receive more data, update the display. | |
369 | syncEmulator = true; | |
370 | } | |
371 | ||
372 | if ((syncEmulator == true) | |
4d2c61b4 KL |
373 | || (display == null) |
374 | ) { | |
375 | // We want to minimize the amount of time we have the emulator | |
376 | // locked. Grab a copy of its display. | |
377 | synchronized (emulator) { | |
378 | // Update the scroll bars | |
379 | reflowData(); | |
380 | ||
1e71bba2 KL |
381 | if (!isDrawable()) { |
382 | // We lost the connection, onShellExit() called an action | |
383 | // that ultimately removed this widget from the UI | |
384 | // hierarchy, so no one cares if we update the display. | |
385 | // Bail out. | |
386 | return; | |
387 | } | |
388 | ||
9917c620 KL |
389 | if ((display == null) || emulator.isReading()) { |
390 | display = emulator.getVisibleDisplay(getHeight(), | |
391 | -getVerticalValue()); | |
392 | assert (display.size() == getHeight()); | |
4d2c61b4 KL |
393 | } |
394 | width = emulator.getWidth(); | |
395 | } | |
396 | dirty = false; | |
397 | } | |
398 | ||
4d2c61b4 KL |
399 | // Now draw the emulator screen |
400 | int row = 0; | |
9917c620 | 401 | for (DisplayLine line: display) { |
4d2c61b4 KL |
402 | int widthMax = width; |
403 | if (line.isDoubleWidth()) { | |
404 | widthMax /= 2; | |
405 | } | |
406 | if (widthMax > getWidth()) { | |
407 | widthMax = getWidth(); | |
408 | } | |
409 | for (int i = 0; i < widthMax; i++) { | |
410 | Cell ch = line.charAt(i); | |
411 | ||
412 | if (ch.isImage()) { | |
413 | putCharXY(i, row, ch); | |
414 | continue; | |
415 | } | |
416 | ||
417 | Cell newCell = new Cell(ch); | |
418 | boolean reverse = line.isReverseColor() ^ ch.isReverse(); | |
419 | newCell.setReverse(false); | |
420 | if (reverse) { | |
421 | if (ch.getForeColorRGB() < 0) { | |
422 | newCell.setBackColor(ch.getForeColor()); | |
423 | newCell.setBackColorRGB(-1); | |
424 | } else { | |
425 | newCell.setBackColorRGB(ch.getForeColorRGB()); | |
426 | } | |
427 | if (ch.getBackColorRGB() < 0) { | |
428 | newCell.setForeColor(ch.getBackColor()); | |
429 | newCell.setForeColorRGB(-1); | |
430 | } else { | |
431 | newCell.setForeColorRGB(ch.getBackColorRGB()); | |
432 | } | |
433 | } | |
434 | if (line.isDoubleWidth()) { | |
435 | putDoubleWidthCharXY(line, (i * 2), row, newCell); | |
436 | } else { | |
437 | putCharXY(i, row, newCell); | |
438 | } | |
439 | } | |
440 | row++; | |
4d2c61b4 | 441 | } |
4d2c61b4 KL |
442 | } |
443 | ||
444 | /** | |
2bc32111 | 445 | * Handle widget close. |
4d2c61b4 KL |
446 | */ |
447 | @Override | |
448 | public void close() { | |
449 | emulator.close(); | |
450 | if (shell != null) { | |
451 | terminateShellChildProcess(); | |
452 | shell.destroy(); | |
453 | shell = null; | |
454 | } | |
455 | } | |
456 | ||
457 | /** | |
458 | * Handle window/screen resize events. | |
459 | * | |
460 | * @param resize resize event | |
461 | */ | |
462 | @Override | |
463 | public void onResize(final TResizeEvent resize) { | |
2bc32111 KL |
464 | // Let TWidget set my size. |
465 | super.onResize(resize); | |
4d2c61b4 KL |
466 | |
467 | // Synchronize against the emulator so we don't stomp on its reader | |
468 | // thread. | |
469 | synchronized (emulator) { | |
470 | ||
471 | if (resize.getType() == TResizeEvent.Type.WIDGET) { | |
472 | // Resize the scroll bars | |
473 | reflowData(); | |
474 | placeScrollbars(); | |
475 | ||
476 | // Get out of scrollback | |
477 | setVerticalValue(0); | |
478 | ||
479 | if (ptypipe) { | |
480 | emulator.setWidth(getWidth()); | |
481 | emulator.setHeight(getHeight()); | |
482 | ||
483 | emulator.writeRemote("\033[8;" + getHeight() + ";" + | |
484 | getWidth() + "t"); | |
485 | } | |
c7a75ad3 KL |
486 | |
487 | // Pass the correct text cell width/height to the emulator | |
488 | if (getScreen() != null) { | |
489 | emulator.setTextWidth(getScreen().getTextWidth()); | |
490 | emulator.setTextHeight(getScreen().getTextHeight()); | |
491 | } | |
4d2c61b4 KL |
492 | } |
493 | return; | |
494 | ||
495 | } // synchronized (emulator) | |
496 | } | |
497 | ||
498 | /** | |
499 | * Resize scrollbars for a new width/height. | |
500 | */ | |
501 | @Override | |
502 | public void reflowData() { | |
503 | ||
504 | // Synchronize against the emulator so we don't stomp on its reader | |
505 | // thread. | |
506 | synchronized (emulator) { | |
507 | ||
508 | // Pull cursor information | |
509 | readEmulatorState(); | |
510 | ||
511 | // Vertical scrollbar | |
512 | setTopValue(getHeight() | |
513 | - (emulator.getScrollbackBuffer().size() | |
514 | + emulator.getDisplayBuffer().size())); | |
515 | setVerticalBigChange(getHeight()); | |
516 | ||
517 | } // synchronized (emulator) | |
518 | } | |
519 | ||
520 | /** | |
521 | * Handle keystrokes. | |
522 | * | |
523 | * @param keypress keystroke event | |
524 | */ | |
525 | @Override | |
526 | public void onKeypress(final TKeypressEvent keypress) { | |
527 | if (hideMouseWhenTyping) { | |
528 | typingHidMouse = true; | |
529 | } | |
530 | ||
531 | // Scrollback up/down | |
532 | if (keypress.equals(kbShiftPgUp) | |
533 | || keypress.equals(kbCtrlPgUp) | |
534 | || keypress.equals(kbAltPgUp) | |
535 | ) { | |
536 | bigVerticalDecrement(); | |
9917c620 | 537 | dirty = true; |
4d2c61b4 KL |
538 | return; |
539 | } | |
540 | if (keypress.equals(kbShiftPgDn) | |
541 | || keypress.equals(kbCtrlPgDn) | |
542 | || keypress.equals(kbAltPgDn) | |
543 | ) { | |
544 | bigVerticalIncrement(); | |
9917c620 | 545 | dirty = true; |
4d2c61b4 KL |
546 | return; |
547 | } | |
548 | ||
549 | if (emulator.isReading()) { | |
550 | // Get out of scrollback | |
551 | setVerticalValue(0); | |
552 | emulator.addUserEvent(keypress); | |
553 | ||
554 | // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if | |
555 | // this is kBEnter then also send kbCtrlJ. | |
556 | if (keypress.equals(kbEnter)) { | |
557 | if (System.getProperty("os.name").startsWith("Windows") | |
558 | && (System.getProperty("jexer.TTerminal.cmdHack", | |
559 | "true").equals("true")) | |
560 | ) { | |
561 | emulator.addUserEvent(new TKeypressEvent(kbCtrlJ)); | |
562 | } | |
563 | } | |
564 | ||
565 | readEmulatorState(); | |
566 | return; | |
567 | } | |
568 | ||
569 | // Process is closed, honor "normal" TUI keystrokes | |
570 | super.onKeypress(keypress); | |
571 | } | |
572 | ||
573 | /** | |
574 | * Handle mouse press events. | |
575 | * | |
576 | * @param mouse mouse button press event | |
577 | */ | |
578 | @Override | |
579 | public void onMouseDown(final TMouseEvent mouse) { | |
580 | if (hideMouseWhenTyping) { | |
581 | typingHidMouse = false; | |
582 | } | |
583 | ||
584 | // If the emulator is tracking mouse buttons, it needs to see wheel | |
585 | // events. | |
586 | if (emulator.getMouseProtocol() == ECMA48.MouseProtocol.OFF) { | |
587 | if (mouse.isMouseWheelUp()) { | |
588 | verticalDecrement(); | |
9917c620 | 589 | dirty = true; |
4d2c61b4 KL |
590 | return; |
591 | } | |
592 | if (mouse.isMouseWheelDown()) { | |
593 | verticalIncrement(); | |
9917c620 | 594 | dirty = true; |
4d2c61b4 KL |
595 | return; |
596 | } | |
597 | } | |
598 | if (mouseOnEmulator(mouse)) { | |
599 | emulator.addUserEvent(mouse); | |
600 | readEmulatorState(); | |
601 | return; | |
602 | } | |
603 | ||
604 | // Emulator didn't consume it, pass it on | |
605 | super.onMouseDown(mouse); | |
606 | } | |
607 | ||
608 | /** | |
609 | * Handle mouse release events. | |
610 | * | |
611 | * @param mouse mouse button release event | |
612 | */ | |
613 | @Override | |
614 | public void onMouseUp(final TMouseEvent mouse) { | |
615 | if (hideMouseWhenTyping) { | |
616 | typingHidMouse = false; | |
617 | } | |
618 | ||
619 | if (mouseOnEmulator(mouse)) { | |
620 | emulator.addUserEvent(mouse); | |
621 | readEmulatorState(); | |
622 | return; | |
623 | } | |
624 | ||
625 | // Emulator didn't consume it, pass it on | |
626 | super.onMouseUp(mouse); | |
627 | } | |
628 | ||
629 | /** | |
630 | * Handle mouse motion events. | |
631 | * | |
632 | * @param mouse mouse motion event | |
633 | */ | |
634 | @Override | |
635 | public void onMouseMotion(final TMouseEvent mouse) { | |
636 | if (hideMouseWhenTyping) { | |
637 | typingHidMouse = false; | |
638 | } | |
639 | ||
640 | if (mouseOnEmulator(mouse)) { | |
641 | emulator.addUserEvent(mouse); | |
642 | readEmulatorState(); | |
643 | return; | |
644 | } | |
645 | ||
646 | // Emulator didn't consume it, pass it on | |
647 | super.onMouseMotion(mouse); | |
648 | } | |
649 | ||
650 | // ------------------------------------------------------------------------ | |
651 | // TTerminalWidget -------------------------------------------------------- | |
652 | // ------------------------------------------------------------------------ | |
653 | ||
654 | /** | |
655 | * Get the desired window title. | |
656 | * | |
657 | * @return the title | |
658 | */ | |
659 | public String getTitle() { | |
660 | return title; | |
661 | } | |
662 | ||
663 | /** | |
2bc32111 | 664 | * Returns true if this widget does not want the application-wide mouse |
4d2c61b4 KL |
665 | * cursor drawn over it. |
666 | * | |
2bc32111 | 667 | * @return true if this widget does not want the application-wide mouse |
4d2c61b4 KL |
668 | * cursor drawn over it |
669 | */ | |
670 | public boolean hasHiddenMouse() { | |
671 | return (emulator.hasHiddenMousePointer() || typingHidMouse); | |
672 | } | |
673 | ||
674 | /** | |
675 | * See if the terminal is still running. | |
676 | * | |
677 | * @return if true, we are still connected to / reading from the remote | |
678 | * side | |
679 | */ | |
680 | public boolean isReading() { | |
681 | return emulator.isReading(); | |
682 | } | |
683 | ||
684 | /** | |
685 | * Convert a string array to a whitespace-separated string. | |
686 | * | |
687 | * @param array the string array | |
688 | * @return a single string | |
689 | */ | |
690 | private String stringArrayToString(final String [] array) { | |
691 | StringBuilder sb = new StringBuilder(array[0].length()); | |
692 | for (int i = 0; i < array.length; i++) { | |
693 | sb.append(array[i]); | |
694 | if (i < array.length - 1) { | |
695 | sb.append(' '); | |
696 | } | |
697 | } | |
698 | return sb.toString(); | |
699 | } | |
700 | ||
701 | /** | |
702 | * Spawn the shell. | |
703 | * | |
704 | * @param command the command line to execute | |
705 | */ | |
706 | private void spawnShell(final String [] command) { | |
707 | ||
708 | /* | |
709 | System.err.printf("spawnShell(): '%s'\n", | |
710 | stringArrayToString(command)); | |
711 | */ | |
712 | ||
713 | // We will have vScroller for its data fields and mouse event | |
714 | // handling, but do not want to draw it. | |
715 | vScroller = new TVScroller(null, getWidth(), 0, getHeight()); | |
716 | vScroller.setVisible(false); | |
717 | setBottomValue(0); | |
718 | ||
719 | title = i18n.getString("windowTitle"); | |
720 | ||
721 | // Assume XTERM | |
722 | ECMA48.DeviceType deviceType = ECMA48.DeviceType.XTERM; | |
723 | ||
724 | try { | |
725 | ProcessBuilder pb = new ProcessBuilder(command); | |
726 | Map<String, String> env = pb.environment(); | |
727 | env.put("TERM", ECMA48.deviceTypeTerm(deviceType)); | |
728 | env.put("LANG", ECMA48.deviceTypeLang(deviceType, "en")); | |
729 | env.put("COLUMNS", "80"); | |
730 | env.put("LINES", "24"); | |
731 | pb.redirectErrorStream(true); | |
732 | shell = pb.start(); | |
733 | emulator = new ECMA48(deviceType, shell.getInputStream(), | |
734 | shell.getOutputStream(), this); | |
735 | } catch (IOException e) { | |
736 | messageBox(i18n.getString("errorLaunchingShellTitle"), | |
737 | MessageFormat.format(i18n.getString("errorLaunchingShellText"), | |
738 | e.getMessage())); | |
739 | } | |
740 | ||
741 | // Setup the scroll bars | |
742 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(), | |
743 | getHeight())); | |
744 | ||
4d2c61b4 KL |
745 | // Hide mouse when typing option |
746 | if (System.getProperty("jexer.TTerminal.hideMouseWhenTyping", | |
747 | "true").equals("false")) { | |
748 | ||
749 | hideMouseWhenTyping = false; | |
750 | } | |
751 | } | |
752 | ||
753 | /** | |
754 | * Terminate the child of the 'script' process used on POSIX. This may | |
755 | * or may not work. | |
756 | */ | |
757 | private void terminateShellChildProcess() { | |
758 | int pid = -1; | |
759 | if (shell.getClass().getName().equals("java.lang.UNIXProcess")) { | |
760 | /* get the PID on unix/linux systems */ | |
761 | try { | |
762 | Field field = shell.getClass().getDeclaredField("pid"); | |
763 | field.setAccessible(true); | |
764 | pid = field.getInt(shell); | |
765 | } catch (Throwable e) { | |
766 | // SQUASH, this didn't work. Just bail out quietly. | |
767 | return; | |
768 | } | |
769 | } | |
770 | if (pid != -1) { | |
771 | // shell.destroy() works successfully at killing this side of | |
772 | // 'script'. But we need to make sure the other side (child | |
773 | // process) is also killed. | |
774 | String [] cmdKillIt = { | |
775 | "pkill", "-P", Integer.toString(pid) | |
776 | }; | |
777 | try { | |
778 | Runtime.getRuntime().exec(cmdKillIt); | |
779 | } catch (Throwable e) { | |
780 | // SQUASH, this didn't work. Just bail out quietly. | |
781 | return; | |
782 | } | |
783 | } | |
784 | } | |
785 | ||
786 | /** | |
787 | * Hook for subclasses to be notified of the shell termination. | |
788 | */ | |
789 | public void onShellExit() { | |
2bc32111 KL |
790 | TApplication app = getApplication(); |
791 | if (app != null) { | |
1e71bba2 KL |
792 | if (closeAction != null) { |
793 | // We have to put this action inside invokeLater() because it | |
794 | // could be executed during draw() when syncing with ECMA48. | |
795 | app.invokeLater(new Runnable() { | |
796 | public void run() { | |
a524aa2e | 797 | closeAction.DO(TTerminalWidget.this); |
2bc32111 | 798 | } |
1e71bba2 KL |
799 | }); |
800 | } | |
801 | if (getApplication() != null) { | |
802 | getApplication().postEvent(new TMenuEvent( | |
803 | TMenu.MID_REPAINT)); | |
804 | } | |
4d2c61b4 | 805 | } |
4d2c61b4 KL |
806 | } |
807 | ||
808 | /** | |
809 | * Copy out variables from the emulator that TTerminal has to expose on | |
810 | * screen. | |
811 | */ | |
812 | private void readEmulatorState() { | |
813 | // Synchronize against the emulator so we don't stomp on its reader | |
814 | // thread. | |
815 | synchronized (emulator) { | |
816 | ||
817 | setCursorX(emulator.getCursorX()); | |
818 | setCursorY(emulator.getCursorY() | |
819 | + (getHeight() - emulator.getHeight()) | |
820 | - getVerticalValue()); | |
821 | setCursorVisible(emulator.isCursorVisible()); | |
822 | if (getCursorX() > getWidth()) { | |
823 | setCursorVisible(false); | |
824 | } | |
825 | if ((getCursorY() >= getHeight()) || (getCursorY() < 0)) { | |
826 | setCursorVisible(false); | |
827 | } | |
828 | if (emulator.getScreenTitle().length() > 0) { | |
829 | // Only update the title if the shell is still alive | |
830 | if (shell != null) { | |
831 | title = emulator.getScreenTitle(); | |
832 | } | |
833 | } | |
834 | ||
835 | // Check to see if the shell has died. | |
836 | if (!emulator.isReading() && (shell != null)) { | |
837 | try { | |
838 | int rc = shell.exitValue(); | |
839 | // The emulator exited on its own, all is fine | |
840 | title = MessageFormat.format(i18n. | |
841 | getString("windowTitleCompleted"), title, rc); | |
842 | exitValue = rc; | |
843 | shell = null; | |
844 | emulator.close(); | |
845 | onShellExit(); | |
846 | } catch (IllegalThreadStateException e) { | |
847 | // The emulator thread has exited, but the shell Process | |
848 | // hasn't figured that out yet. Do nothing, we will see | |
849 | // this in a future tick. | |
850 | } | |
851 | } else if (emulator.isReading() && (shell != null)) { | |
852 | // The shell might be dead, let's check | |
853 | try { | |
854 | int rc = shell.exitValue(); | |
855 | // If we got here, the shell died. | |
856 | title = MessageFormat.format(i18n. | |
857 | getString("windowTitleCompleted"), title, rc); | |
858 | exitValue = rc; | |
859 | shell = null; | |
860 | emulator.close(); | |
861 | onShellExit(); | |
862 | } catch (IllegalThreadStateException e) { | |
863 | // The shell is still running, do nothing. | |
864 | } | |
865 | } | |
866 | ||
867 | } // synchronized (emulator) | |
868 | } | |
869 | ||
870 | /** | |
871 | * Check if a mouse press/release/motion event coordinate is over the | |
872 | * emulator. | |
873 | * | |
874 | * @param mouse a mouse-based event | |
875 | * @return whether or not the mouse is on the emulator | |
876 | */ | |
877 | private boolean mouseOnEmulator(final TMouseEvent mouse) { | |
878 | ||
879 | if (!emulator.isReading()) { | |
880 | return false; | |
881 | } | |
882 | ||
883 | if ((mouse.getX() >= 0) | |
884 | && (mouse.getX() < getWidth() - 1) | |
885 | && (mouse.getY() >= 0) | |
886 | && (mouse.getY() < getHeight()) | |
887 | ) { | |
888 | return true; | |
889 | } | |
890 | return false; | |
891 | } | |
892 | ||
4d2c61b4 KL |
893 | /** |
894 | * Draw glyphs for a double-width or double-height VT100 cell to two | |
895 | * screen cells. | |
896 | * | |
897 | * @param line the line this VT100 cell is in | |
898 | * @param x the X position to draw the left half to | |
899 | * @param y the Y position to draw to | |
900 | * @param cell the cell to draw | |
901 | */ | |
902 | private void putDoubleWidthCharXY(final DisplayLine line, final int x, | |
903 | final int y, final Cell cell) { | |
904 | ||
905 | int textWidth = getScreen().getTextWidth(); | |
906 | int textHeight = getScreen().getTextHeight(); | |
907 | boolean cursorBlinkVisible = true; | |
908 | ||
909 | if (getScreen() instanceof SwingTerminal) { | |
910 | SwingTerminal terminal = (SwingTerminal) getScreen(); | |
911 | cursorBlinkVisible = terminal.getCursorBlinkVisible(); | |
912 | } else if (getScreen() instanceof ECMA48Terminal) { | |
913 | ECMA48Terminal terminal = (ECMA48Terminal) getScreen(); | |
914 | ||
915 | if (!terminal.hasSixel()) { | |
916 | // The backend does not have sixel support, draw this as text | |
917 | // and bail out. | |
918 | putCharXY(x, y, cell); | |
919 | putCharXY(x + 1, y, ' ', cell); | |
920 | return; | |
921 | } | |
922 | cursorBlinkVisible = blinkState; | |
923 | } else { | |
924 | // We don't know how to dray glyphs to this screen, draw them as | |
925 | // text and bail out. | |
926 | putCharXY(x, y, cell); | |
927 | putCharXY(x + 1, y, ' ', cell); | |
928 | return; | |
929 | } | |
930 | ||
931 | if ((textWidth != lastTextWidth) || (textHeight != lastTextHeight)) { | |
932 | // Screen size has changed, reset the font. | |
933 | setupFont(textHeight); | |
934 | lastTextWidth = textWidth; | |
935 | lastTextHeight = textHeight; | |
936 | } | |
937 | assert (doubleFont != null); | |
938 | ||
939 | BufferedImage image; | |
940 | if (line.getDoubleHeight() == 1) { | |
941 | // Double-height top half: don't draw the underline. | |
942 | Cell newCell = new Cell(cell); | |
943 | newCell.setUnderline(false); | |
944 | image = doubleFont.getImage(newCell, textWidth * 2, textHeight * 2, | |
945 | cursorBlinkVisible); | |
946 | } else { | |
947 | image = doubleFont.getImage(cell, textWidth * 2, textHeight * 2, | |
948 | cursorBlinkVisible); | |
949 | } | |
950 | ||
951 | // Now that we have the double-wide glyph drawn, copy the right | |
952 | // pieces of it to the cells. | |
953 | Cell left = new Cell(cell); | |
954 | Cell right = new Cell(cell); | |
955 | right.setChar(' '); | |
956 | BufferedImage leftImage = null; | |
957 | BufferedImage rightImage = null; | |
958 | /* | |
959 | System.err.println("image " + image + " textWidth " + textWidth + | |
960 | " textHeight " + textHeight); | |
961 | */ | |
962 | ||
963 | switch (line.getDoubleHeight()) { | |
964 | case 1: | |
965 | // Top half double height | |
966 | leftImage = image.getSubimage(0, 0, textWidth, textHeight); | |
967 | rightImage = image.getSubimage(textWidth, 0, textWidth, textHeight); | |
968 | break; | |
969 | case 2: | |
970 | // Bottom half double height | |
971 | leftImage = image.getSubimage(0, textHeight, textWidth, textHeight); | |
972 | rightImage = image.getSubimage(textWidth, textHeight, | |
973 | textWidth, textHeight); | |
974 | break; | |
975 | default: | |
976 | // Either single height double-width, or error fallback | |
977 | BufferedImage wideImage = new BufferedImage(textWidth * 2, | |
978 | textHeight, BufferedImage.TYPE_INT_ARGB); | |
979 | Graphics2D grWide = wideImage.createGraphics(); | |
980 | grWide.drawImage(image, 0, 0, wideImage.getWidth(), | |
981 | wideImage.getHeight(), null); | |
982 | grWide.dispose(); | |
983 | leftImage = wideImage.getSubimage(0, 0, textWidth, textHeight); | |
984 | rightImage = wideImage.getSubimage(textWidth, 0, textWidth, | |
985 | textHeight); | |
986 | break; | |
987 | } | |
988 | left.setImage(leftImage); | |
989 | right.setImage(rightImage); | |
990 | // Since we have image data, ditch the character here. Otherwise, a | |
991 | // drawBoxShadow() over the terminal window will show the characters | |
992 | // which looks wrong. | |
993 | left.setChar(' '); | |
994 | right.setChar(' '); | |
995 | putCharXY(x, y, left); | |
996 | putCharXY(x + 1, y, right); | |
997 | } | |
998 | ||
999 | /** | |
1000 | * Set up the double-width font. | |
1001 | * | |
1002 | * @param fontSize the size of font to request for the single-width font. | |
1003 | * The double-width font will be 2x this value. | |
1004 | */ | |
1005 | private void setupFont(final int fontSize) { | |
1006 | doubleFont = GlyphMaker.getInstance(fontSize * 2); | |
1007 | ||
1008 | // Special case: the ECMA48 backend needs to have a timer to drive | |
1009 | // its blink state. | |
1010 | if (getScreen() instanceof jexer.backend.ECMA48Terminal) { | |
1011 | if (!haveTimer) { | |
1012 | // Blink every 500 millis. | |
1013 | long millis = 500; | |
1014 | getApplication().addTimer(millis, true, | |
1015 | new TAction() { | |
1016 | public void DO() { | |
1017 | blinkState = !blinkState; | |
1018 | getApplication().doRepaint(); | |
1019 | } | |
1020 | } | |
1021 | ); | |
1022 | haveTimer = true; | |
1023 | } | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | // ------------------------------------------------------------------------ | |
1028 | // DisplayListener -------------------------------------------------------- | |
1029 | // ------------------------------------------------------------------------ | |
1030 | ||
1031 | /** | |
1032 | * Called by emulator when fresh data has come in. | |
1033 | */ | |
1034 | public void displayChanged() { | |
1035 | dirty = true; | |
1036 | getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT)); | |
1037 | } | |
1038 | ||
1039 | /** | |
1040 | * Function to call to obtain the display width. | |
1041 | * | |
1042 | * @return the number of columns in the display | |
1043 | */ | |
1044 | public int getDisplayWidth() { | |
1045 | if (ptypipe) { | |
1046 | return getWidth(); | |
1047 | } | |
1048 | return 80; | |
1049 | } | |
1050 | ||
1051 | /** | |
1052 | * Function to call to obtain the display height. | |
1053 | * | |
1054 | * @return the number of rows in the display | |
1055 | */ | |
1056 | public int getDisplayHeight() { | |
1057 | if (ptypipe) { | |
1058 | return getHeight(); | |
1059 | } | |
1060 | return 24; | |
1061 | } | |
1062 | ||
1063 | } |