| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2017 Kevin Lamonte |
| 7 | * |
| 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: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 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. |
| 25 | * |
| 26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 27 | * @version 1 |
| 28 | */ |
| 29 | package jexer; |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | import java.lang.reflect.Field; |
| 33 | import java.text.MessageFormat; |
| 34 | import java.util.LinkedList; |
| 35 | import java.util.List; |
| 36 | import java.util.Map; |
| 37 | import java.util.ResourceBundle; |
| 38 | |
| 39 | import jexer.bits.Cell; |
| 40 | import jexer.bits.CellAttributes; |
| 41 | import jexer.event.TKeypressEvent; |
| 42 | import jexer.event.TMenuEvent; |
| 43 | import jexer.event.TMouseEvent; |
| 44 | import jexer.event.TResizeEvent; |
| 45 | import jexer.menu.TMenu; |
| 46 | import jexer.tterminal.DisplayLine; |
| 47 | import jexer.tterminal.DisplayListener; |
| 48 | import jexer.tterminal.ECMA48; |
| 49 | import static jexer.TKeypress.*; |
| 50 | |
| 51 | /** |
| 52 | * TTerminalWindow exposes a ECMA-48 / ANSI X3.64 style terminal in a window. |
| 53 | */ |
| 54 | public class TTerminalWindow extends TScrollableWindow |
| 55 | implements DisplayListener { |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Translated strings. |
| 60 | */ |
| 61 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TTerminalWindow.class.getName()); |
| 62 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 167 | /** |
| 168 | * Spawn the shell. |
| 169 | * |
| 170 | * @param command the command line to execute |
| 171 | */ |
| 172 | private void spawnShell(final String [] command) { |
| 173 | |
| 174 | /* |
| 175 | System.err.printf("spawnShell(): '%s'\n", |
| 176 | stringArrayToString(command)); |
| 177 | */ |
| 178 | |
| 179 | vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2); |
| 180 | setBottomValue(0); |
| 181 | |
| 182 | // Assume XTERM |
| 183 | ECMA48.DeviceType deviceType = ECMA48.DeviceType.XTERM; |
| 184 | |
| 185 | try { |
| 186 | ProcessBuilder pb = new ProcessBuilder(command); |
| 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"); |
| 192 | pb.redirectErrorStream(true); |
| 193 | shell = pb.start(); |
| 194 | emulator = new ECMA48(deviceType, shell.getInputStream(), |
| 195 | shell.getOutputStream(), this); |
| 196 | } catch (IOException e) { |
| 197 | messageBox(i18n.getString("errorLaunchingShellTitle"), |
| 198 | MessageFormat.format(i18n.getString("errorLaunchingShellText"), |
| 199 | e.getMessage())); |
| 200 | } |
| 201 | |
| 202 | // Setup the scroll bars |
| 203 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(), |
| 204 | getHeight())); |
| 205 | |
| 206 | // Claim the keystrokes the emulator will need. |
| 207 | addShortcutKeys(); |
| 208 | |
| 209 | // Add shortcut text |
| 210 | newStatusBar(i18n.getString("statusBarRunning")); |
| 211 | } |
| 212 | |
| 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 | |
| 224 | this(application, x, y, RESIZABLE, commandLine.split("\\s")); |
| 225 | } |
| 226 | |
| 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 |
| 234 | * @param command the command line to execute |
| 235 | */ |
| 236 | public TTerminalWindow(final TApplication application, final int x, |
| 237 | final int y, final int flags, final String [] command) { |
| 238 | |
| 239 | super(application, i18n.getString("windowTitle"), x, y, |
| 240 | 80 + 2, 24 + 2, flags); |
| 241 | |
| 242 | String [] fullCommand; |
| 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; |
| 250 | fullCommand = new String[command.length + 1]; |
| 251 | fullCommand[0] = "ptypipe"; |
| 252 | System.arraycopy(command, 0, fullCommand, 1, command.length); |
| 253 | } else if (System.getProperty("os.name").startsWith("Windows")) { |
| 254 | fullCommand = new String[3]; |
| 255 | fullCommand[0] = "cmd"; |
| 256 | fullCommand[1] = "/c"; |
| 257 | fullCommand[2] = stringArrayToString(command); |
| 258 | } else if (System.getProperty("os.name").startsWith("Mac")) { |
| 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); |
| 266 | } else { |
| 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); |
| 274 | } |
| 275 | spawnShell(fullCommand); |
| 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; |
| 311 | spawnShell(cmdShellPtypipe.split("\\s")); |
| 312 | } else if (System.getProperty("os.name").startsWith("Windows")) { |
| 313 | spawnShell(cmdShellWindows.split("\\s")); |
| 314 | } else if (System.getProperty("os.name").startsWith("Mac")) { |
| 315 | spawnShell(cmdShellBSD.split("\\s")); |
| 316 | } else if (System.getProperty("os.name").startsWith("Linux")) { |
| 317 | spawnShell(cmdShellGNU.split("\\s")); |
| 318 | } else { |
| 319 | // When all else fails, assume GNU. |
| 320 | spawnShell(cmdShellGNU.split("\\s")); |
| 321 | } |
| 322 | } |
| 323 | |
| 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 | |
| 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 |
| 367 | reflowData(); |
| 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 |
| 376 | int visibleHeight = getHeight() - 2; |
| 377 | int visibleBottom = scrollback.size() + display.size() |
| 378 | + getVerticalValue(); |
| 379 | assert (visibleBottom >= 0); |
| 380 | |
| 381 | List<DisplayLine> preceedingBlankLines = new LinkedList<DisplayLine>(); |
| 382 | int visibleTop = visibleBottom - visibleHeight; |
| 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); |
| 394 | |
| 395 | List<DisplayLine> visibleLines = new LinkedList<DisplayLine>(); |
| 396 | visibleLines.addAll(preceedingBlankLines); |
| 397 | visibleLines.addAll(displayLines.subList(visibleTop, |
| 398 | visibleBottom)); |
| 399 | |
| 400 | visibleHeight -= visibleLines.size(); |
| 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); |
| 417 | boolean reverse = line.isReverseColor() ^ ch.isReverse(); |
| 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 | |
| 447 | /** |
| 448 | * Called by emulator when fresh data has come in. |
| 449 | */ |
| 450 | public void displayChanged() { |
| 451 | getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT)); |
| 452 | } |
| 453 | |
| 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() { |
| 460 | if (ptypipe) { |
| 461 | return getWidth() - 2; |
| 462 | } |
| 463 | return 80; |
| 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() { |
| 472 | if (ptypipe) { |
| 473 | return getHeight() - 2; |
| 474 | } |
| 475 | return 24; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Handle window close. |
| 480 | */ |
| 481 | @Override |
| 482 | public void onClose() { |
| 483 | emulator.close(); |
| 484 | if (shell != null) { |
| 485 | terminateShellChildProcess(); |
| 486 | shell.destroy(); |
| 487 | shell = null; |
| 488 | } |
| 489 | } |
| 490 | |
| 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 | |
| 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 |
| 509 | + (getHeight() - 2 - emulator.getHeight()) |
| 510 | - getVerticalValue()); |
| 511 | setCursorVisible(emulator.isCursorVisible()); |
| 512 | if (getCursorX() > getWidth() - 2) { |
| 513 | setCursorVisible(false); |
| 514 | } |
| 515 | if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) { |
| 516 | setCursorVisible(false); |
| 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 | } |
| 524 | |
| 525 | // Check to see if the shell has died. |
| 526 | if (!emulator.isReading() && (shell != null)) { |
| 527 | try { |
| 528 | int rc = shell.exitValue(); |
| 529 | // The emulator exited on its own, all is fine |
| 530 | setTitle(MessageFormat.format(i18n. |
| 531 | getString("windowTitleCompleted"), getTitle(), rc)); |
| 532 | shell = null; |
| 533 | emulator.close(); |
| 534 | clearShortcutKeypresses(); |
| 535 | statusBar.setText(MessageFormat.format(i18n. |
| 536 | getString("statusBarCompleted"), rc)); |
| 537 | onShellExit(); |
| 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 | } |
| 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. |
| 548 | setTitle(MessageFormat.format(i18n. |
| 549 | getString("windowTitleCompleted"), getTitle(), rc)); |
| 550 | shell = null; |
| 551 | emulator.close(); |
| 552 | clearShortcutKeypresses(); |
| 553 | statusBar.setText(MessageFormat.format(i18n. |
| 554 | getString("statusBarCompleted"), rc)); |
| 555 | onShellExit(); |
| 556 | } catch (IllegalThreadStateException e) { |
| 557 | // The shell is still running, do nothing. |
| 558 | } |
| 559 | } |
| 560 | |
| 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 |
| 578 | reflowData(); |
| 579 | placeScrollbars(); |
| 580 | |
| 581 | // Get out of scrollback |
| 582 | setVerticalValue(0); |
| 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 | } |
| 591 | } |
| 592 | return; |
| 593 | |
| 594 | } // synchronized (emulator) |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Resize scrollbars for a new width/height. |
| 599 | */ |
| 600 | @Override |
| 601 | public void reflowData() { |
| 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 |
| 611 | setTopValue(getHeight() - 2 |
| 612 | - (emulator.getScrollbackBuffer().size() |
| 613 | + emulator.getDisplayBuffer().size())); |
| 614 | setVerticalBigChange(getHeight() - 2); |
| 615 | |
| 616 | } // synchronized (emulator) |
| 617 | } |
| 618 | |
| 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 | |
| 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 | ) { |
| 657 | bigVerticalDecrement(); |
| 658 | return; |
| 659 | } |
| 660 | if (keypress.equals(kbShiftPgDn) |
| 661 | || keypress.equals(kbCtrlPgDn) |
| 662 | || keypress.equals(kbAltPgDn) |
| 663 | ) { |
| 664 | bigVerticalIncrement(); |
| 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 |
| 673 | setVerticalValue(0); |
| 674 | emulator.keypress(keypress.getKey()); |
| 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 | |
| 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) { |
| 700 | if (inWindowMove || inWindowResize) { |
| 701 | // TWindow needs to deal with this. |
| 702 | super.onMouseDown(mouse); |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | if (mouse.isMouseWheelUp()) { |
| 707 | verticalDecrement(); |
| 708 | return; |
| 709 | } |
| 710 | if (mouse.isMouseWheelDown()) { |
| 711 | verticalIncrement(); |
| 712 | return; |
| 713 | } |
| 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 | } |
| 723 | |
| 724 | // Emulator didn't consume it, pass it on |
| 725 | super.onMouseDown(mouse); |
| 726 | } |
| 727 | |
| 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 | |
| 782 | } |