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