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