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