Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
34a42e78 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
34a42e78 | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 Kevin Lamonte |
34a42e78 | 7 | * |
e16dda65 KL |
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: | |
34a42e78 | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
34a42e78 | 17 | * |
e16dda65 KL |
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. | |
34a42e78 KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
34a42e78 | 31 | import java.io.IOException; |
55d2b2c2 | 32 | import java.lang.reflect.Field; |
339652cc | 33 | import java.text.MessageFormat; |
34a42e78 KL |
34 | import java.util.LinkedList; |
35 | import java.util.List; | |
bd8d51fa | 36 | import java.util.Map; |
339652cc | 37 | import java.util.ResourceBundle; |
34a42e78 KL |
38 | |
39 | import jexer.bits.Cell; | |
40 | import jexer.bits.CellAttributes; | |
41 | import jexer.event.TKeypressEvent; | |
b2d49e0f | 42 | import jexer.event.TMenuEvent; |
34a42e78 KL |
43 | import jexer.event.TMouseEvent; |
44 | import jexer.event.TResizeEvent; | |
b2d49e0f | 45 | import jexer.menu.TMenu; |
34a42e78 | 46 | import jexer.tterminal.DisplayLine; |
be72cb5c | 47 | import jexer.tterminal.DisplayListener; |
34a42e78 KL |
48 | import jexer.tterminal.ECMA48; |
49 | import static jexer.TKeypress.*; | |
50 | ||
51 | /** | |
52 | * TTerminalWindow exposes a ECMA-48 / ANSI X3.64 style terminal in a window. | |
53 | */ | |
be72cb5c KL |
54 | public class TTerminalWindow extends TScrollableWindow |
55 | implements DisplayListener { | |
34a42e78 | 56 | |
339652cc KL |
57 | |
58 | /** | |
59 | * Translated strings. | |
60 | */ | |
61 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TTerminalWindow.class.getName()); | |
62 | ||
34a42e78 KL |
63 | /** |
64 | * The emulator. | |
65 | */ | |
66 | private ECMA48 emulator; | |
67 | ||
68 | /** | |
69 | * The Process created by the shell spawning constructor. | |
70 | */ | |
71 | private Process shell; | |
72 | ||
1d99a38f KL |
73 | /** |
74 | * If true, we are using the ptypipe utility to support dynamic window | |
75 | * resizing. ptypipe is available at | |
76 | * https://github.com/klamonte/ptypipe . | |
77 | */ | |
78 | private boolean ptypipe = false; | |
79 | ||
5dfd1c11 KL |
80 | /** |
81 | * Claim the keystrokes the emulator will need. | |
82 | */ | |
83 | private void addShortcutKeys() { | |
84 | addShortcutKeypress(kbCtrlA); | |
85 | addShortcutKeypress(kbCtrlB); | |
86 | addShortcutKeypress(kbCtrlC); | |
87 | addShortcutKeypress(kbCtrlD); | |
88 | addShortcutKeypress(kbCtrlE); | |
89 | addShortcutKeypress(kbCtrlF); | |
90 | addShortcutKeypress(kbCtrlG); | |
91 | addShortcutKeypress(kbCtrlH); | |
92 | addShortcutKeypress(kbCtrlU); | |
93 | addShortcutKeypress(kbCtrlJ); | |
94 | addShortcutKeypress(kbCtrlK); | |
95 | addShortcutKeypress(kbCtrlL); | |
96 | addShortcutKeypress(kbCtrlM); | |
97 | addShortcutKeypress(kbCtrlN); | |
98 | addShortcutKeypress(kbCtrlO); | |
99 | addShortcutKeypress(kbCtrlP); | |
100 | addShortcutKeypress(kbCtrlQ); | |
101 | addShortcutKeypress(kbCtrlR); | |
102 | addShortcutKeypress(kbCtrlS); | |
103 | addShortcutKeypress(kbCtrlT); | |
104 | addShortcutKeypress(kbCtrlU); | |
105 | addShortcutKeypress(kbCtrlV); | |
106 | addShortcutKeypress(kbCtrlW); | |
107 | addShortcutKeypress(kbCtrlX); | |
108 | addShortcutKeypress(kbCtrlY); | |
109 | addShortcutKeypress(kbCtrlZ); | |
110 | addShortcutKeypress(kbF1); | |
111 | addShortcutKeypress(kbF2); | |
112 | addShortcutKeypress(kbF3); | |
113 | addShortcutKeypress(kbF4); | |
114 | addShortcutKeypress(kbF5); | |
115 | addShortcutKeypress(kbF6); | |
116 | addShortcutKeypress(kbF7); | |
117 | addShortcutKeypress(kbF8); | |
118 | addShortcutKeypress(kbF9); | |
119 | addShortcutKeypress(kbF10); | |
120 | addShortcutKeypress(kbF11); | |
121 | addShortcutKeypress(kbF12); | |
122 | addShortcutKeypress(kbAltA); | |
123 | addShortcutKeypress(kbAltB); | |
124 | addShortcutKeypress(kbAltC); | |
125 | addShortcutKeypress(kbAltD); | |
126 | addShortcutKeypress(kbAltE); | |
127 | addShortcutKeypress(kbAltF); | |
128 | addShortcutKeypress(kbAltG); | |
129 | addShortcutKeypress(kbAltH); | |
130 | addShortcutKeypress(kbAltU); | |
131 | addShortcutKeypress(kbAltJ); | |
132 | addShortcutKeypress(kbAltK); | |
133 | addShortcutKeypress(kbAltL); | |
134 | addShortcutKeypress(kbAltM); | |
135 | addShortcutKeypress(kbAltN); | |
136 | addShortcutKeypress(kbAltO); | |
137 | addShortcutKeypress(kbAltP); | |
138 | addShortcutKeypress(kbAltQ); | |
139 | addShortcutKeypress(kbAltR); | |
140 | addShortcutKeypress(kbAltS); | |
141 | addShortcutKeypress(kbAltT); | |
142 | addShortcutKeypress(kbAltU); | |
143 | addShortcutKeypress(kbAltV); | |
144 | addShortcutKeypress(kbAltW); | |
145 | addShortcutKeypress(kbAltX); | |
146 | addShortcutKeypress(kbAltY); | |
147 | addShortcutKeypress(kbAltZ); | |
148 | } | |
149 | ||
34a42e78 | 150 | /** |
6f8ff91a | 151 | * Spawn the shell. |
34a42e78 | 152 | * |
6f8ff91a | 153 | * @param commandLine the command line to execute |
34a42e78 | 154 | */ |
6f8ff91a | 155 | private void spawnShell(final String commandLine) { |
34a42e78 | 156 | |
56661844 KL |
157 | vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2); |
158 | setBottomValue(0); | |
159 | ||
bd8d51fa KL |
160 | // Assume XTERM |
161 | ECMA48.DeviceType deviceType = ECMA48.DeviceType.XTERM; | |
162 | ||
34a42e78 | 163 | try { |
6f8ff91a KL |
164 | String [] command = commandLine.split("\\s"); |
165 | ProcessBuilder pb = new ProcessBuilder(command); | |
bd8d51fa KL |
166 | Map<String, String> env = pb.environment(); |
167 | env.put("TERM", ECMA48.deviceTypeTerm(deviceType)); | |
168 | env.put("LANG", ECMA48.deviceTypeLang(deviceType, "en")); | |
169 | env.put("COLUMNS", "80"); | |
170 | env.put("LINES", "24"); | |
34a42e78 KL |
171 | pb.redirectErrorStream(true); |
172 | shell = pb.start(); | |
bd8d51fa | 173 | emulator = new ECMA48(deviceType, shell.getInputStream(), |
aa77d682 | 174 | shell.getOutputStream(), this); |
34a42e78 | 175 | } catch (IOException e) { |
339652cc KL |
176 | messageBox(i18n.getString("errorLaunchingShellTitle"), |
177 | MessageFormat.format(i18n.getString("errorLaunchingShellText"), | |
178 | e.getMessage())); | |
34a42e78 KL |
179 | } |
180 | ||
181 | // Setup the scroll bars | |
182 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(), | |
183 | getHeight())); | |
5dfd1c11 KL |
184 | |
185 | // Claim the keystrokes the emulator will need. | |
186 | addShortcutKeys(); | |
2ce6dab2 KL |
187 | |
188 | // Add shortcut text | |
339652cc | 189 | newStatusBar(i18n.getString("statusBarRunning")); |
34a42e78 KL |
190 | } |
191 | ||
b2d49e0f KL |
192 | /** |
193 | * Public constructor spawns a custom command line. | |
194 | * | |
195 | * @param application TApplication that manages this window | |
196 | * @param x column relative to parent | |
197 | * @param y row relative to parent | |
198 | * @param commandLine the command line to execute | |
199 | */ | |
200 | public TTerminalWindow(final TApplication application, final int x, | |
201 | final int y, final String commandLine) { | |
202 | ||
203 | this(application, x, y, RESIZABLE, commandLine); | |
204 | } | |
205 | ||
6f8ff91a KL |
206 | /** |
207 | * Public constructor spawns a custom command line. | |
208 | * | |
209 | * @param application TApplication that manages this window | |
210 | * @param x column relative to parent | |
211 | * @param y row relative to parent | |
212 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
213 | * @param commandLine the command line to execute | |
214 | */ | |
215 | public TTerminalWindow(final TApplication application, final int x, | |
216 | final int y, final int flags, final String commandLine) { | |
217 | ||
218 | super(application, i18n.getString("windowTitle"), x, y, | |
219 | 80 + 2, 24 + 2, flags); | |
220 | ||
221 | String cmdShellWindows = "cmd.exe /c" + commandLine; | |
222 | String cmdShellGNU = "script -fqe /dev/null -c " + commandLine; | |
223 | String cmdShellBSD = "script -q -F /dev/null -c " + commandLine; | |
224 | String cmdShellPtypipe = "ptypipe " + commandLine; | |
225 | ||
226 | // Spawn a shell and pass its I/O to the other constructor. | |
227 | if ((System.getProperty("jexer.TTerminal.ptypipe") != null) | |
228 | && (System.getProperty("jexer.TTerminal.ptypipe"). | |
229 | equals("true")) | |
230 | ) { | |
231 | ptypipe = true; | |
232 | spawnShell(cmdShellPtypipe); | |
233 | } else if (System.getProperty("os.name").startsWith("Windows")) { | |
234 | spawnShell(cmdShellWindows); | |
235 | } else if (System.getProperty("os.name").startsWith("Mac")) { | |
236 | spawnShell(cmdShellBSD); | |
237 | } else if (System.getProperty("os.name").startsWith("Linux")) { | |
238 | spawnShell(cmdShellGNU); | |
239 | } else { | |
240 | // When all else fails, assume GNU. | |
241 | spawnShell(cmdShellGNU); | |
242 | } | |
243 | } | |
244 | ||
245 | /** | |
246 | * Public constructor spawns a shell. | |
247 | * | |
248 | * @param application TApplication that manages this window | |
249 | * @param x column relative to parent | |
250 | * @param y row relative to parent | |
251 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
252 | */ | |
253 | public TTerminalWindow(final TApplication application, final int x, | |
254 | final int y, final int flags) { | |
255 | ||
256 | super(application, i18n.getString("windowTitle"), x, y, | |
257 | 80 + 2, 24 + 2, flags); | |
258 | ||
259 | String cmdShellWindows = "cmd.exe"; | |
260 | ||
261 | // You cannot run a login shell in a bare Process interactively, due | |
262 | // to libc's behavior of buffering when stdin/stdout aren't a tty. | |
263 | // Use 'script' instead to run a shell in a pty. And because BSD and | |
264 | // GNU differ on the '-f' vs '-F' flags, we need two different | |
265 | // commands. Lovely. | |
266 | String cmdShellGNU = "script -fqe /dev/null"; | |
267 | String cmdShellBSD = "script -q -F /dev/null"; | |
268 | ||
269 | // ptypipe is another solution that permits dynamic window resizing. | |
270 | String cmdShellPtypipe = "ptypipe /bin/bash --login"; | |
271 | ||
272 | // Spawn a shell and pass its I/O to the other constructor. | |
273 | if ((System.getProperty("jexer.TTerminal.ptypipe") != null) | |
274 | && (System.getProperty("jexer.TTerminal.ptypipe"). | |
275 | equals("true")) | |
276 | ) { | |
277 | ptypipe = true; | |
278 | spawnShell(cmdShellPtypipe); | |
279 | } else if (System.getProperty("os.name").startsWith("Windows")) { | |
280 | spawnShell(cmdShellWindows); | |
281 | } else if (System.getProperty("os.name").startsWith("Mac")) { | |
282 | spawnShell(cmdShellBSD); | |
283 | } else if (System.getProperty("os.name").startsWith("Linux")) { | |
284 | spawnShell(cmdShellGNU); | |
285 | } else { | |
286 | // When all else fails, assume GNU. | |
287 | spawnShell(cmdShellGNU); | |
288 | } | |
289 | } | |
290 | ||
55d2b2c2 KL |
291 | /** |
292 | * Terminate the child of the 'script' process used on POSIX. This may | |
293 | * or may not work. | |
294 | */ | |
295 | private void terminateShellChildProcess() { | |
296 | int pid = -1; | |
297 | if (shell.getClass().getName().equals("java.lang.UNIXProcess")) { | |
298 | /* get the PID on unix/linux systems */ | |
299 | try { | |
300 | Field field = shell.getClass().getDeclaredField("pid"); | |
301 | field.setAccessible(true); | |
302 | pid = field.getInt(shell); | |
303 | } catch (Throwable e) { | |
304 | // SQUASH, this didn't work. Just bail out quietly. | |
305 | return; | |
306 | } | |
307 | } | |
308 | if (pid != -1) { | |
309 | // shell.destroy() works successfully at killing this side of | |
310 | // 'script'. But we need to make sure the other side (child | |
311 | // process) is also killed. | |
312 | String [] cmdKillIt = { | |
313 | "pkill", "-P", Integer.toString(pid) | |
314 | }; | |
315 | try { | |
316 | Runtime.getRuntime().exec(cmdKillIt); | |
317 | } catch (Throwable e) { | |
318 | // SQUASH, this didn't work. Just bail out quietly. | |
319 | return; | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
34a42e78 KL |
324 | /** |
325 | * Draw the display buffer. | |
326 | */ | |
327 | @Override | |
328 | public void draw() { | |
329 | // Synchronize against the emulator so we don't stomp on its reader | |
330 | // thread. | |
331 | synchronized (emulator) { | |
332 | ||
333 | // Update the scroll bars | |
56661844 | 334 | reflowData(); |
34a42e78 KL |
335 | |
336 | // Draw the box using my superclass | |
337 | super.draw(); | |
338 | ||
339 | List<DisplayLine> scrollback = emulator.getScrollbackBuffer(); | |
340 | List<DisplayLine> display = emulator.getDisplayBuffer(); | |
341 | ||
342 | // Put together the visible rows | |
34a42e78 | 343 | int visibleHeight = getHeight() - 2; |
34a42e78 | 344 | int visibleBottom = scrollback.size() + display.size() |
56661844 | 345 | + getVerticalValue(); |
34a42e78 KL |
346 | assert (visibleBottom >= 0); |
347 | ||
348 | List<DisplayLine> preceedingBlankLines = new LinkedList<DisplayLine>(); | |
349 | int visibleTop = visibleBottom - visibleHeight; | |
34a42e78 KL |
350 | if (visibleTop < 0) { |
351 | for (int i = visibleTop; i < 0; i++) { | |
352 | preceedingBlankLines.add(emulator.getBlankDisplayLine()); | |
353 | } | |
354 | visibleTop = 0; | |
355 | } | |
356 | assert (visibleTop >= 0); | |
357 | ||
358 | List<DisplayLine> displayLines = new LinkedList<DisplayLine>(); | |
359 | displayLines.addAll(scrollback); | |
360 | displayLines.addAll(display); | |
34a42e78 KL |
361 | |
362 | List<DisplayLine> visibleLines = new LinkedList<DisplayLine>(); | |
363 | visibleLines.addAll(preceedingBlankLines); | |
364 | visibleLines.addAll(displayLines.subList(visibleTop, | |
365 | visibleBottom)); | |
34a42e78 KL |
366 | |
367 | visibleHeight -= visibleLines.size(); | |
34a42e78 KL |
368 | assert (visibleHeight >= 0); |
369 | ||
370 | // Now draw the emulator screen | |
371 | int row = 1; | |
372 | for (DisplayLine line: visibleLines) { | |
373 | int widthMax = emulator.getWidth(); | |
374 | if (line.isDoubleWidth()) { | |
375 | widthMax /= 2; | |
376 | } | |
377 | if (widthMax > getWidth() - 2) { | |
378 | widthMax = getWidth() - 2; | |
379 | } | |
380 | for (int i = 0; i < widthMax; i++) { | |
381 | Cell ch = line.charAt(i); | |
382 | Cell newCell = new Cell(); | |
383 | newCell.setTo(ch); | |
7c870d89 | 384 | boolean reverse = line.isReverseColor() ^ ch.isReverse(); |
34a42e78 KL |
385 | newCell.setReverse(false); |
386 | if (reverse) { | |
387 | newCell.setBackColor(ch.getForeColor()); | |
388 | newCell.setForeColor(ch.getBackColor()); | |
389 | } | |
390 | if (line.isDoubleWidth()) { | |
391 | getScreen().putCharXY((i * 2) + 1, row, newCell); | |
392 | getScreen().putCharXY((i * 2) + 2, row, ' ', newCell); | |
393 | } else { | |
394 | getScreen().putCharXY(i + 1, row, newCell); | |
395 | } | |
396 | } | |
397 | row++; | |
398 | if (row == getHeight() - 1) { | |
399 | // Don't overwrite the box edge | |
400 | break; | |
401 | } | |
402 | } | |
403 | CellAttributes background = new CellAttributes(); | |
404 | // Fill in the blank lines on bottom | |
405 | for (int i = 0; i < visibleHeight; i++) { | |
406 | getScreen().hLineXY(1, i + row, getWidth() - 2, ' ', | |
407 | background); | |
408 | } | |
409 | ||
410 | } // synchronized (emulator) | |
411 | ||
412 | } | |
413 | ||
be72cb5c KL |
414 | /** |
415 | * Called by emulator when fresh data has come in. | |
416 | */ | |
417 | public void displayChanged() { | |
b2d49e0f | 418 | getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT)); |
be72cb5c KL |
419 | } |
420 | ||
aa77d682 KL |
421 | /** |
422 | * Function to call to obtain the display width. | |
423 | * | |
424 | * @return the number of columns in the display | |
425 | */ | |
426 | public int getDisplayWidth() { | |
00fbfc38 KL |
427 | if (ptypipe) { |
428 | return getWidth() - 2; | |
429 | } | |
430 | return 80; | |
aa77d682 KL |
431 | } |
432 | ||
433 | /** | |
434 | * Function to call to obtain the display height. | |
435 | * | |
436 | * @return the number of rows in the display | |
437 | */ | |
438 | public int getDisplayHeight() { | |
00fbfc38 KL |
439 | if (ptypipe) { |
440 | return getHeight() - 2; | |
441 | } | |
442 | return 24; | |
aa77d682 KL |
443 | } |
444 | ||
34a42e78 KL |
445 | /** |
446 | * Handle window close. | |
447 | */ | |
2ce6dab2 KL |
448 | @Override |
449 | public void onClose() { | |
5dfd1c11 | 450 | emulator.close(); |
69345248 | 451 | if (shell != null) { |
55d2b2c2 | 452 | terminateShellChildProcess(); |
69345248 KL |
453 | shell.destroy(); |
454 | shell = null; | |
69345248 | 455 | } |
34a42e78 KL |
456 | } |
457 | ||
b2d49e0f KL |
458 | /** |
459 | * Hook for subclasses to be notified of the shell termination. | |
460 | */ | |
461 | public void onShellExit() { | |
462 | getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT)); | |
463 | } | |
464 | ||
34a42e78 KL |
465 | /** |
466 | * Copy out variables from the emulator that TTerminal has to expose on | |
467 | * screen. | |
468 | */ | |
469 | private void readEmulatorState() { | |
470 | // Synchronize against the emulator so we don't stomp on its reader | |
471 | // thread. | |
472 | synchronized (emulator) { | |
473 | ||
474 | setCursorX(emulator.getCursorX() + 1); | |
475 | setCursorY(emulator.getCursorY() + 1 | |
56661844 KL |
476 | + (getHeight() - 2 - emulator.getHeight()) |
477 | - getVerticalValue()); | |
7c870d89 | 478 | setCursorVisible(emulator.isCursorVisible()); |
34a42e78 | 479 | if (getCursorX() > getWidth() - 2) { |
7c870d89 | 480 | setCursorVisible(false); |
34a42e78 KL |
481 | } |
482 | if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) { | |
7c870d89 | 483 | setCursorVisible(false); |
34a42e78 KL |
484 | } |
485 | if (emulator.getScreenTitle().length() > 0) { | |
486 | // Only update the title if the shell is still alive | |
487 | if (shell != null) { | |
488 | setTitle(emulator.getScreenTitle()); | |
489 | } | |
490 | } | |
34a42e78 KL |
491 | |
492 | // Check to see if the shell has died. | |
493 | if (!emulator.isReading() && (shell != null)) { | |
55b4f29b KL |
494 | try { |
495 | int rc = shell.exitValue(); | |
496 | // The emulator exited on its own, all is fine | |
339652cc KL |
497 | setTitle(MessageFormat.format(i18n. |
498 | getString("windowTitleCompleted"), getTitle(), rc)); | |
55b4f29b KL |
499 | shell = null; |
500 | emulator.close(); | |
5dfd1c11 | 501 | clearShortcutKeypresses(); |
339652cc KL |
502 | statusBar.setText(MessageFormat.format(i18n. |
503 | getString("statusBarCompleted"), rc)); | |
b2d49e0f | 504 | onShellExit(); |
55b4f29b KL |
505 | } catch (IllegalThreadStateException e) { |
506 | // The emulator thread has exited, but the shell Process | |
507 | // hasn't figured that out yet. Do nothing, we will see | |
508 | // this in a future tick. | |
509 | } | |
34a42e78 KL |
510 | } else if (emulator.isReading() && (shell != null)) { |
511 | // The shell might be dead, let's check | |
512 | try { | |
513 | int rc = shell.exitValue(); | |
514 | // If we got here, the shell died. | |
339652cc KL |
515 | setTitle(MessageFormat.format(i18n. |
516 | getString("windowTitleCompleted"), getTitle(), rc)); | |
34a42e78 KL |
517 | shell = null; |
518 | emulator.close(); | |
5dfd1c11 | 519 | clearShortcutKeypresses(); |
339652cc KL |
520 | statusBar.setText(MessageFormat.format(i18n. |
521 | getString("statusBarCompleted"), rc)); | |
b2d49e0f | 522 | onShellExit(); |
34a42e78 KL |
523 | } catch (IllegalThreadStateException e) { |
524 | // The shell is still running, do nothing. | |
525 | } | |
526 | } | |
92554d64 | 527 | |
34a42e78 KL |
528 | } // synchronized (emulator) |
529 | } | |
530 | ||
531 | /** | |
532 | * Handle window/screen resize events. | |
533 | * | |
534 | * @param resize resize event | |
535 | */ | |
536 | @Override | |
537 | public void onResize(final TResizeEvent resize) { | |
538 | ||
539 | // Synchronize against the emulator so we don't stomp on its reader | |
540 | // thread. | |
541 | synchronized (emulator) { | |
542 | ||
543 | if (resize.getType() == TResizeEvent.Type.WIDGET) { | |
544 | // Resize the scroll bars | |
56661844 KL |
545 | reflowData(); |
546 | placeScrollbars(); | |
34a42e78 KL |
547 | |
548 | // Get out of scrollback | |
56661844 | 549 | setVerticalValue(0); |
1d99a38f KL |
550 | |
551 | if (ptypipe) { | |
552 | emulator.setWidth(getWidth() - 2); | |
553 | emulator.setHeight(getHeight() - 2); | |
554 | ||
555 | emulator.writeRemote("\033[8;" + (getHeight() - 2) + ";" + | |
556 | (getWidth() - 2) + "t"); | |
557 | } | |
34a42e78 KL |
558 | } |
559 | return; | |
560 | ||
561 | } // synchronized (emulator) | |
562 | } | |
563 | ||
564 | /** | |
565 | * Resize scrollbars for a new width/height. | |
566 | */ | |
56661844 KL |
567 | @Override |
568 | public void reflowData() { | |
34a42e78 KL |
569 | |
570 | // Synchronize against the emulator so we don't stomp on its reader | |
571 | // thread. | |
572 | synchronized (emulator) { | |
573 | ||
574 | // Pull cursor information | |
575 | readEmulatorState(); | |
576 | ||
577 | // Vertical scrollbar | |
56661844 | 578 | setTopValue(getHeight() - 2 |
34a42e78 KL |
579 | - (emulator.getScrollbackBuffer().size() |
580 | + emulator.getDisplayBuffer().size())); | |
56661844 | 581 | setVerticalBigChange(getHeight() - 2); |
34a42e78 KL |
582 | |
583 | } // synchronized (emulator) | |
584 | } | |
585 | ||
bd8d51fa KL |
586 | /** |
587 | * Check if a mouse press/release/motion event coordinate is over the | |
588 | * emulator. | |
589 | * | |
590 | * @param mouse a mouse-based event | |
591 | * @return whether or not the mouse is on the emulator | |
592 | */ | |
593 | private final boolean mouseOnEmulator(final TMouseEvent mouse) { | |
594 | ||
595 | synchronized (emulator) { | |
596 | if (!emulator.isReading()) { | |
597 | return false; | |
598 | } | |
599 | } | |
600 | ||
601 | if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1) | |
602 | && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1) | |
603 | && (mouse.getAbsoluteY() >= getAbsoluteY() + 1) | |
604 | && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1) | |
605 | ) { | |
606 | return true; | |
607 | } | |
608 | return false; | |
609 | } | |
610 | ||
34a42e78 KL |
611 | /** |
612 | * Handle keystrokes. | |
613 | * | |
614 | * @param keypress keystroke event | |
615 | */ | |
616 | @Override | |
617 | public void onKeypress(final TKeypressEvent keypress) { | |
618 | ||
619 | // Scrollback up/down | |
620 | if (keypress.equals(kbShiftPgUp) | |
621 | || keypress.equals(kbCtrlPgUp) | |
622 | || keypress.equals(kbAltPgUp) | |
623 | ) { | |
56661844 | 624 | bigVerticalDecrement(); |
34a42e78 KL |
625 | return; |
626 | } | |
627 | if (keypress.equals(kbShiftPgDn) | |
628 | || keypress.equals(kbCtrlPgDn) | |
629 | || keypress.equals(kbAltPgDn) | |
630 | ) { | |
56661844 | 631 | bigVerticalIncrement(); |
34a42e78 KL |
632 | return; |
633 | } | |
634 | ||
635 | // Synchronize against the emulator so we don't stomp on its reader | |
636 | // thread. | |
637 | synchronized (emulator) { | |
638 | if (emulator.isReading()) { | |
639 | // Get out of scrollback | |
56661844 | 640 | setVerticalValue(0); |
34a42e78 | 641 | emulator.keypress(keypress.getKey()); |
92554d64 KL |
642 | |
643 | // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if | |
644 | // this is kBEnter then also send kbCtrlJ. | |
645 | if (System.getProperty("os.name").startsWith("Windows")) { | |
646 | if (keypress.equals(kbEnter)) { | |
647 | emulator.keypress(kbCtrlJ); | |
648 | } | |
649 | } | |
650 | ||
34a42e78 KL |
651 | readEmulatorState(); |
652 | return; | |
653 | } | |
654 | } | |
655 | ||
656 | // Process is closed, honor "normal" TUI keystrokes | |
657 | super.onKeypress(keypress); | |
658 | } | |
659 | ||
660 | /** | |
661 | * Handle mouse press events. | |
662 | * | |
663 | * @param mouse mouse button press event | |
664 | */ | |
665 | @Override | |
666 | public void onMouseDown(final TMouseEvent mouse) { | |
bd8d51fa KL |
667 | if (inWindowMove || inWindowResize) { |
668 | // TWindow needs to deal with this. | |
669 | super.onMouseDown(mouse); | |
670 | return; | |
671 | } | |
34a42e78 | 672 | |
7c870d89 | 673 | if (mouse.isMouseWheelUp()) { |
56661844 | 674 | verticalDecrement(); |
34a42e78 KL |
675 | return; |
676 | } | |
7c870d89 | 677 | if (mouse.isMouseWheelDown()) { |
56661844 | 678 | verticalIncrement(); |
34a42e78 KL |
679 | return; |
680 | } | |
bd8d51fa KL |
681 | if (mouseOnEmulator(mouse)) { |
682 | synchronized (emulator) { | |
683 | mouse.setX(mouse.getX() - 1); | |
684 | mouse.setY(mouse.getY() - 1); | |
685 | emulator.mouse(mouse); | |
686 | readEmulatorState(); | |
687 | return; | |
688 | } | |
689 | } | |
34a42e78 | 690 | |
bd8d51fa | 691 | // Emulator didn't consume it, pass it on |
34a42e78 KL |
692 | super.onMouseDown(mouse); |
693 | } | |
694 | ||
bd8d51fa KL |
695 | /** |
696 | * Handle mouse release events. | |
697 | * | |
698 | * @param mouse mouse button release event | |
699 | */ | |
700 | @Override | |
701 | public void onMouseUp(final TMouseEvent mouse) { | |
702 | if (inWindowMove || inWindowResize) { | |
703 | // TWindow needs to deal with this. | |
704 | super.onMouseUp(mouse); | |
705 | return; | |
706 | } | |
707 | ||
708 | if (mouseOnEmulator(mouse)) { | |
709 | synchronized (emulator) { | |
710 | mouse.setX(mouse.getX() - 1); | |
711 | mouse.setY(mouse.getY() - 1); | |
712 | emulator.mouse(mouse); | |
713 | readEmulatorState(); | |
714 | return; | |
715 | } | |
716 | } | |
717 | ||
718 | // Emulator didn't consume it, pass it on | |
719 | super.onMouseUp(mouse); | |
720 | } | |
721 | ||
722 | /** | |
723 | * Handle mouse motion events. | |
724 | * | |
725 | * @param mouse mouse motion event | |
726 | */ | |
727 | @Override | |
728 | public void onMouseMotion(final TMouseEvent mouse) { | |
729 | if (inWindowMove || inWindowResize) { | |
730 | // TWindow needs to deal with this. | |
731 | super.onMouseMotion(mouse); | |
732 | return; | |
733 | } | |
734 | ||
735 | if (mouseOnEmulator(mouse)) { | |
736 | synchronized (emulator) { | |
737 | mouse.setX(mouse.getX() - 1); | |
738 | mouse.setY(mouse.getY() - 1); | |
739 | emulator.mouse(mouse); | |
740 | readEmulatorState(); | |
741 | return; | |
742 | } | |
743 | } | |
744 | ||
745 | // Emulator didn't consume it, pass it on | |
746 | super.onMouseMotion(mouse); | |
747 | } | |
748 | ||
34a42e78 | 749 | } |