#34 call onResize() when window size is changed
[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) {
051e2913
KL
420 if (ch.getForeColorRGB() < 0) {
421 newCell.setBackColor(ch.getForeColor());
422 newCell.setBackColorRGB(-1);
423 } else {
424 newCell.setBackColorRGB(ch.getForeColorRGB());
425 }
426 if (ch.getBackColorRGB() < 0) {
427 newCell.setForeColor(ch.getBackColor());
428 newCell.setForeColorRGB(-1);
429 } else {
430 newCell.setForeColorRGB(ch.getBackColorRGB());
431 }
34a42e78
KL
432 }
433 if (line.isDoubleWidth()) {
434 getScreen().putCharXY((i * 2) + 1, row, newCell);
435 getScreen().putCharXY((i * 2) + 2, row, ' ', newCell);
436 } else {
437 getScreen().putCharXY(i + 1, row, newCell);
438 }
439 }
440 row++;
441 if (row == getHeight() - 1) {
442 // Don't overwrite the box edge
443 break;
444 }
445 }
446 CellAttributes background = new CellAttributes();
447 // Fill in the blank lines on bottom
448 for (int i = 0; i < visibleHeight; i++) {
449 getScreen().hLineXY(1, i + row, getWidth() - 2, ' ',
450 background);
451 }
452
453 } // synchronized (emulator)
454
455 }
456
be72cb5c
KL
457 /**
458 * Called by emulator when fresh data has come in.
459 */
460 public void displayChanged() {
b2d49e0f 461 getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
be72cb5c
KL
462 }
463
aa77d682
KL
464 /**
465 * Function to call to obtain the display width.
466 *
467 * @return the number of columns in the display
468 */
469 public int getDisplayWidth() {
00fbfc38
KL
470 if (ptypipe) {
471 return getWidth() - 2;
472 }
473 return 80;
aa77d682
KL
474 }
475
476 /**
477 * Function to call to obtain the display height.
478 *
479 * @return the number of rows in the display
480 */
481 public int getDisplayHeight() {
00fbfc38
KL
482 if (ptypipe) {
483 return getHeight() - 2;
484 }
485 return 24;
aa77d682
KL
486 }
487
34a42e78
KL
488 /**
489 * Handle window close.
490 */
2ce6dab2
KL
491 @Override
492 public void onClose() {
5dfd1c11 493 emulator.close();
69345248 494 if (shell != null) {
55d2b2c2 495 terminateShellChildProcess();
69345248
KL
496 shell.destroy();
497 shell = null;
69345248 498 }
34a42e78
KL
499 }
500
b2d49e0f
KL
501 /**
502 * Hook for subclasses to be notified of the shell termination.
503 */
504 public void onShellExit() {
505 getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
506 }
507
34a42e78
KL
508 /**
509 * Copy out variables from the emulator that TTerminal has to expose on
510 * screen.
511 */
512 private void readEmulatorState() {
513 // Synchronize against the emulator so we don't stomp on its reader
514 // thread.
515 synchronized (emulator) {
516
517 setCursorX(emulator.getCursorX() + 1);
518 setCursorY(emulator.getCursorY() + 1
56661844
KL
519 + (getHeight() - 2 - emulator.getHeight())
520 - getVerticalValue());
7c870d89 521 setCursorVisible(emulator.isCursorVisible());
34a42e78 522 if (getCursorX() > getWidth() - 2) {
7c870d89 523 setCursorVisible(false);
34a42e78
KL
524 }
525 if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
7c870d89 526 setCursorVisible(false);
34a42e78
KL
527 }
528 if (emulator.getScreenTitle().length() > 0) {
529 // Only update the title if the shell is still alive
530 if (shell != null) {
531 setTitle(emulator.getScreenTitle());
532 }
533 }
34a42e78
KL
534
535 // Check to see if the shell has died.
536 if (!emulator.isReading() && (shell != null)) {
55b4f29b
KL
537 try {
538 int rc = shell.exitValue();
539 // The emulator exited on its own, all is fine
339652cc
KL
540 setTitle(MessageFormat.format(i18n.
541 getString("windowTitleCompleted"), getTitle(), rc));
55b4f29b
KL
542 shell = null;
543 emulator.close();
5dfd1c11 544 clearShortcutKeypresses();
339652cc
KL
545 statusBar.setText(MessageFormat.format(i18n.
546 getString("statusBarCompleted"), rc));
b2d49e0f 547 onShellExit();
55b4f29b
KL
548 } catch (IllegalThreadStateException e) {
549 // The emulator thread has exited, but the shell Process
550 // hasn't figured that out yet. Do nothing, we will see
551 // this in a future tick.
552 }
34a42e78
KL
553 } else if (emulator.isReading() && (shell != null)) {
554 // The shell might be dead, let's check
555 try {
556 int rc = shell.exitValue();
557 // If we got here, the shell died.
339652cc
KL
558 setTitle(MessageFormat.format(i18n.
559 getString("windowTitleCompleted"), getTitle(), rc));
34a42e78
KL
560 shell = null;
561 emulator.close();
5dfd1c11 562 clearShortcutKeypresses();
339652cc
KL
563 statusBar.setText(MessageFormat.format(i18n.
564 getString("statusBarCompleted"), rc));
b2d49e0f 565 onShellExit();
34a42e78
KL
566 } catch (IllegalThreadStateException e) {
567 // The shell is still running, do nothing.
568 }
569 }
92554d64 570
34a42e78
KL
571 } // synchronized (emulator)
572 }
573
574 /**
575 * Handle window/screen resize events.
576 *
577 * @param resize resize event
578 */
579 @Override
580 public void onResize(final TResizeEvent resize) {
581
582 // Synchronize against the emulator so we don't stomp on its reader
583 // thread.
584 synchronized (emulator) {
585
586 if (resize.getType() == TResizeEvent.Type.WIDGET) {
587 // Resize the scroll bars
56661844
KL
588 reflowData();
589 placeScrollbars();
34a42e78
KL
590
591 // Get out of scrollback
56661844 592 setVerticalValue(0);
1d99a38f
KL
593
594 if (ptypipe) {
595 emulator.setWidth(getWidth() - 2);
596 emulator.setHeight(getHeight() - 2);
597
598 emulator.writeRemote("\033[8;" + (getHeight() - 2) + ";" +
599 (getWidth() - 2) + "t");
600 }
34a42e78
KL
601 }
602 return;
603
604 } // synchronized (emulator)
605 }
606
607 /**
608 * Resize scrollbars for a new width/height.
609 */
56661844
KL
610 @Override
611 public void reflowData() {
34a42e78
KL
612
613 // Synchronize against the emulator so we don't stomp on its reader
614 // thread.
615 synchronized (emulator) {
616
617 // Pull cursor information
618 readEmulatorState();
619
620 // Vertical scrollbar
56661844 621 setTopValue(getHeight() - 2
34a42e78
KL
622 - (emulator.getScrollbackBuffer().size()
623 + emulator.getDisplayBuffer().size()));
56661844 624 setVerticalBigChange(getHeight() - 2);
34a42e78
KL
625
626 } // synchronized (emulator)
627 }
628
bd8d51fa
KL
629 /**
630 * Check if a mouse press/release/motion event coordinate is over the
631 * emulator.
632 *
633 * @param mouse a mouse-based event
634 * @return whether or not the mouse is on the emulator
635 */
636 private final boolean mouseOnEmulator(final TMouseEvent mouse) {
637
638 synchronized (emulator) {
639 if (!emulator.isReading()) {
640 return false;
641 }
642 }
643
644 if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
645 && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
646 && (mouse.getAbsoluteY() >= getAbsoluteY() + 1)
647 && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
648 ) {
649 return true;
650 }
651 return false;
652 }
653
34a42e78
KL
654 /**
655 * Handle keystrokes.
656 *
657 * @param keypress keystroke event
658 */
659 @Override
660 public void onKeypress(final TKeypressEvent keypress) {
661
662 // Scrollback up/down
663 if (keypress.equals(kbShiftPgUp)
664 || keypress.equals(kbCtrlPgUp)
665 || keypress.equals(kbAltPgUp)
666 ) {
56661844 667 bigVerticalDecrement();
34a42e78
KL
668 return;
669 }
670 if (keypress.equals(kbShiftPgDn)
671 || keypress.equals(kbCtrlPgDn)
672 || keypress.equals(kbAltPgDn)
673 ) {
56661844 674 bigVerticalIncrement();
34a42e78
KL
675 return;
676 }
677
678 // Synchronize against the emulator so we don't stomp on its reader
679 // thread.
680 synchronized (emulator) {
681 if (emulator.isReading()) {
682 // Get out of scrollback
56661844 683 setVerticalValue(0);
34a42e78 684 emulator.keypress(keypress.getKey());
92554d64
KL
685
686 // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if
687 // this is kBEnter then also send kbCtrlJ.
688 if (System.getProperty("os.name").startsWith("Windows")) {
689 if (keypress.equals(kbEnter)) {
690 emulator.keypress(kbCtrlJ);
691 }
692 }
693
34a42e78
KL
694 readEmulatorState();
695 return;
696 }
697 }
698
699 // Process is closed, honor "normal" TUI keystrokes
700 super.onKeypress(keypress);
701 }
702
703 /**
704 * Handle mouse press events.
705 *
706 * @param mouse mouse button press event
707 */
708 @Override
709 public void onMouseDown(final TMouseEvent mouse) {
bd8d51fa
KL
710 if (inWindowMove || inWindowResize) {
711 // TWindow needs to deal with this.
712 super.onMouseDown(mouse);
713 return;
714 }
34a42e78 715
7c870d89 716 if (mouse.isMouseWheelUp()) {
56661844 717 verticalDecrement();
34a42e78
KL
718 return;
719 }
7c870d89 720 if (mouse.isMouseWheelDown()) {
56661844 721 verticalIncrement();
34a42e78
KL
722 return;
723 }
bd8d51fa
KL
724 if (mouseOnEmulator(mouse)) {
725 synchronized (emulator) {
726 mouse.setX(mouse.getX() - 1);
727 mouse.setY(mouse.getY() - 1);
728 emulator.mouse(mouse);
729 readEmulatorState();
730 return;
731 }
732 }
34a42e78 733
bd8d51fa 734 // Emulator didn't consume it, pass it on
34a42e78
KL
735 super.onMouseDown(mouse);
736 }
737
bd8d51fa
KL
738 /**
739 * Handle mouse release events.
740 *
741 * @param mouse mouse button release event
742 */
743 @Override
744 public void onMouseUp(final TMouseEvent mouse) {
745 if (inWindowMove || inWindowResize) {
746 // TWindow needs to deal with this.
747 super.onMouseUp(mouse);
748 return;
749 }
750
751 if (mouseOnEmulator(mouse)) {
752 synchronized (emulator) {
753 mouse.setX(mouse.getX() - 1);
754 mouse.setY(mouse.getY() - 1);
755 emulator.mouse(mouse);
756 readEmulatorState();
757 return;
758 }
759 }
760
761 // Emulator didn't consume it, pass it on
762 super.onMouseUp(mouse);
763 }
764
765 /**
766 * Handle mouse motion events.
767 *
768 * @param mouse mouse motion event
769 */
770 @Override
771 public void onMouseMotion(final TMouseEvent mouse) {
772 if (inWindowMove || inWindowResize) {
773 // TWindow needs to deal with this.
774 super.onMouseMotion(mouse);
775 return;
776 }
777
778 if (mouseOnEmulator(mouse)) {
779 synchronized (emulator) {
780 mouse.setX(mouse.getX() - 1);
781 mouse.setY(mouse.getY() - 1);
782 emulator.mouse(mouse);
783 readEmulatorState();
784 return;
785 }
786 }
787
788 // Emulator didn't consume it, pass it on
789 super.onMouseMotion(mouse);
790 }
791
34a42e78 792}