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