| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2019 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.awt.image.BufferedImage; |
| 32 | import java.awt.Font; |
| 33 | import java.awt.FontMetrics; |
| 34 | import java.awt.Graphics2D; |
| 35 | |
| 36 | import java.io.InputStream; |
| 37 | import java.io.IOException; |
| 38 | import java.lang.reflect.Field; |
| 39 | import java.text.MessageFormat; |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.HashMap; |
| 42 | import java.util.List; |
| 43 | import java.util.Map; |
| 44 | import java.util.ResourceBundle; |
| 45 | |
| 46 | import jexer.backend.ECMA48Terminal; |
| 47 | import jexer.backend.MultiScreen; |
| 48 | import jexer.backend.SwingTerminal; |
| 49 | import jexer.bits.Cell; |
| 50 | import jexer.bits.CellAttributes; |
| 51 | import jexer.event.TKeypressEvent; |
| 52 | import jexer.event.TMenuEvent; |
| 53 | import jexer.event.TMouseEvent; |
| 54 | import jexer.event.TResizeEvent; |
| 55 | import jexer.menu.TMenu; |
| 56 | import jexer.tterminal.DisplayLine; |
| 57 | import jexer.tterminal.DisplayListener; |
| 58 | import jexer.tterminal.ECMA48; |
| 59 | import static jexer.TKeypress.*; |
| 60 | |
| 61 | /** |
| 62 | * TTerminalWindow exposes a ECMA-48 / ANSI X3.64 style terminal in a window. |
| 63 | */ |
| 64 | public class TTerminalWindow extends TScrollableWindow |
| 65 | implements DisplayListener { |
| 66 | |
| 67 | /** |
| 68 | * Translated strings. |
| 69 | */ |
| 70 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TTerminalWindow.class.getName()); |
| 71 | |
| 72 | // ------------------------------------------------------------------------ |
| 73 | // Variables -------------------------------------------------------------- |
| 74 | // ------------------------------------------------------------------------ |
| 75 | |
| 76 | /** |
| 77 | * The emulator. |
| 78 | */ |
| 79 | private ECMA48 emulator; |
| 80 | |
| 81 | /** |
| 82 | * The Process created by the shell spawning constructor. |
| 83 | */ |
| 84 | private Process shell; |
| 85 | |
| 86 | /** |
| 87 | * If true, we are using the ptypipe utility to support dynamic window |
| 88 | * resizing. ptypipe is available at |
| 89 | * https://gitlab.com/klamonte/ptypipe . |
| 90 | */ |
| 91 | private boolean ptypipe = false; |
| 92 | |
| 93 | /** |
| 94 | * If true, close the window when the shell exits. |
| 95 | */ |
| 96 | private boolean closeOnExit = false; |
| 97 | |
| 98 | /** |
| 99 | * System-dependent Y adjustment for text in the character cell |
| 100 | * (double-height). |
| 101 | */ |
| 102 | private int doubleTextAdjustY = 0; |
| 103 | |
| 104 | /** |
| 105 | * System-dependent X adjustment for text in the character cell |
| 106 | * (double-height). |
| 107 | */ |
| 108 | private int doubleTextAdjustX = 0; |
| 109 | |
| 110 | /** |
| 111 | * Descent of a character cell in pixels (double-height). |
| 112 | */ |
| 113 | private int doubleMaxDescent = 0; |
| 114 | |
| 115 | /** |
| 116 | * Double-width font. |
| 117 | */ |
| 118 | private Font doubleFont = null; |
| 119 | |
| 120 | /** |
| 121 | * Last text width value. |
| 122 | */ |
| 123 | private int lastTextWidth = -1; |
| 124 | |
| 125 | /** |
| 126 | * Last text height value. |
| 127 | */ |
| 128 | private int lastTextHeight = -1; |
| 129 | |
| 130 | /** |
| 131 | * A cache of previously-rendered double-width glyphs. |
| 132 | */ |
| 133 | private Map<Cell, BufferedImage> glyphCache; |
| 134 | |
| 135 | // ------------------------------------------------------------------------ |
| 136 | // Constructors ----------------------------------------------------------- |
| 137 | // ------------------------------------------------------------------------ |
| 138 | |
| 139 | /** |
| 140 | * Public constructor spawns a custom command line. |
| 141 | * |
| 142 | * @param application TApplication that manages this window |
| 143 | * @param x column relative to parent |
| 144 | * @param y row relative to parent |
| 145 | * @param commandLine the command line to execute |
| 146 | */ |
| 147 | public TTerminalWindow(final TApplication application, final int x, |
| 148 | final int y, final String commandLine) { |
| 149 | |
| 150 | this(application, x, y, RESIZABLE, commandLine.split("\\s+"), |
| 151 | System.getProperty("jexer.TTerminal.closeOnExit", |
| 152 | "false").equals("true")); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Public constructor spawns a custom command line. |
| 157 | * |
| 158 | * @param application TApplication that manages this window |
| 159 | * @param x column relative to parent |
| 160 | * @param y row relative to parent |
| 161 | * @param commandLine the command line to execute |
| 162 | * @param closeOnExit if true, close the window when the command exits |
| 163 | */ |
| 164 | public TTerminalWindow(final TApplication application, final int x, |
| 165 | final int y, final String commandLine, final boolean closeOnExit) { |
| 166 | |
| 167 | this(application, x, y, RESIZABLE, commandLine.split("\\s+"), |
| 168 | closeOnExit); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Public constructor spawns a custom command line. |
| 173 | * |
| 174 | * @param application TApplication that manages this window |
| 175 | * @param x column relative to parent |
| 176 | * @param y row relative to parent |
| 177 | * @param flags mask of CENTERED, MODAL, or RESIZABLE |
| 178 | * @param command the command line to execute |
| 179 | */ |
| 180 | public TTerminalWindow(final TApplication application, final int x, |
| 181 | final int y, final int flags, final String [] command) { |
| 182 | |
| 183 | this(application, x, y, flags, command, |
| 184 | System.getProperty("jexer.TTerminal.closeOnExit", |
| 185 | "false").equals("true")); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Public constructor spawns a custom command line. |
| 190 | * |
| 191 | * @param application TApplication that manages this window |
| 192 | * @param x column relative to parent |
| 193 | * @param y row relative to parent |
| 194 | * @param flags mask of CENTERED, MODAL, or RESIZABLE |
| 195 | * @param command the command line to execute |
| 196 | * @param closeOnExit if true, close the window when the command exits |
| 197 | */ |
| 198 | public TTerminalWindow(final TApplication application, final int x, |
| 199 | final int y, final int flags, final String [] command, |
| 200 | final boolean closeOnExit) { |
| 201 | |
| 202 | super(application, i18n.getString("windowTitle"), x, y, |
| 203 | 80 + 2, 24 + 2, flags); |
| 204 | |
| 205 | this.closeOnExit = closeOnExit; |
| 206 | |
| 207 | String [] fullCommand; |
| 208 | |
| 209 | // Spawn a shell and pass its I/O to the other constructor. |
| 210 | if ((System.getProperty("jexer.TTerminal.ptypipe") != null) |
| 211 | && (System.getProperty("jexer.TTerminal.ptypipe"). |
| 212 | equals("true")) |
| 213 | ) { |
| 214 | ptypipe = true; |
| 215 | fullCommand = new String[command.length + 1]; |
| 216 | fullCommand[0] = "ptypipe"; |
| 217 | System.arraycopy(command, 0, fullCommand, 1, command.length); |
| 218 | } else if (System.getProperty("os.name").startsWith("Windows")) { |
| 219 | fullCommand = new String[3]; |
| 220 | fullCommand[0] = "cmd"; |
| 221 | fullCommand[1] = "/c"; |
| 222 | fullCommand[2] = stringArrayToString(command); |
| 223 | } else if (System.getProperty("os.name").startsWith("Mac")) { |
| 224 | fullCommand = new String[6]; |
| 225 | fullCommand[0] = "script"; |
| 226 | fullCommand[1] = "-q"; |
| 227 | fullCommand[2] = "-F"; |
| 228 | fullCommand[3] = "/dev/null"; |
| 229 | fullCommand[4] = "-c"; |
| 230 | fullCommand[5] = stringArrayToString(command); |
| 231 | } else { |
| 232 | // Default: behave like Linux |
| 233 | fullCommand = new String[5]; |
| 234 | fullCommand[0] = "script"; |
| 235 | fullCommand[1] = "-fqe"; |
| 236 | fullCommand[2] = "/dev/null"; |
| 237 | fullCommand[3] = "-c"; |
| 238 | fullCommand[4] = stringArrayToString(command); |
| 239 | } |
| 240 | spawnShell(fullCommand); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Public constructor spawns a shell. |
| 245 | * |
| 246 | * @param application TApplication that manages this window |
| 247 | * @param x column relative to parent |
| 248 | * @param y row relative to parent |
| 249 | * @param flags mask of CENTERED, MODAL, or RESIZABLE |
| 250 | */ |
| 251 | public TTerminalWindow(final TApplication application, final int x, |
| 252 | final int y, final int flags) { |
| 253 | |
| 254 | this(application, x, y, flags, |
| 255 | System.getProperty("jexer.TTerminal.closeOnExit", |
| 256 | "false").equals("true")); |
| 257 | |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Public constructor spawns a shell. |
| 262 | * |
| 263 | * @param application TApplication that manages this window |
| 264 | * @param x column relative to parent |
| 265 | * @param y row relative to parent |
| 266 | * @param flags mask of CENTERED, MODAL, or RESIZABLE |
| 267 | * @param closeOnExit if true, close the window when the shell exits |
| 268 | */ |
| 269 | public TTerminalWindow(final TApplication application, final int x, |
| 270 | final int y, final int flags, final boolean closeOnExit) { |
| 271 | |
| 272 | super(application, i18n.getString("windowTitle"), x, y, |
| 273 | 80 + 2, 24 + 2, flags); |
| 274 | |
| 275 | this.closeOnExit = closeOnExit; |
| 276 | |
| 277 | String cmdShellWindows = "cmd.exe"; |
| 278 | |
| 279 | // You cannot run a login shell in a bare Process interactively, due |
| 280 | // to libc's behavior of buffering when stdin/stdout aren't a tty. |
| 281 | // Use 'script' instead to run a shell in a pty. And because BSD and |
| 282 | // GNU differ on the '-f' vs '-F' flags, we need two different |
| 283 | // commands. Lovely. |
| 284 | String cmdShellGNU = "script -fqe /dev/null"; |
| 285 | String cmdShellBSD = "script -q -F /dev/null"; |
| 286 | |
| 287 | // ptypipe is another solution that permits dynamic window resizing. |
| 288 | String cmdShellPtypipe = "ptypipe /bin/bash --login"; |
| 289 | |
| 290 | // Spawn a shell and pass its I/O to the other constructor. |
| 291 | if ((System.getProperty("jexer.TTerminal.ptypipe") != null) |
| 292 | && (System.getProperty("jexer.TTerminal.ptypipe"). |
| 293 | equals("true")) |
| 294 | ) { |
| 295 | ptypipe = true; |
| 296 | spawnShell(cmdShellPtypipe.split("\\s+")); |
| 297 | } else if (System.getProperty("os.name").startsWith("Windows")) { |
| 298 | spawnShell(cmdShellWindows.split("\\s+")); |
| 299 | } else if (System.getProperty("os.name").startsWith("Mac")) { |
| 300 | spawnShell(cmdShellBSD.split("\\s+")); |
| 301 | } else if (System.getProperty("os.name").startsWith("Linux")) { |
| 302 | spawnShell(cmdShellGNU.split("\\s+")); |
| 303 | } else { |
| 304 | // When all else fails, assume GNU. |
| 305 | spawnShell(cmdShellGNU.split("\\s+")); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // ------------------------------------------------------------------------ |
| 310 | // TScrollableWindow ------------------------------------------------------ |
| 311 | // ------------------------------------------------------------------------ |
| 312 | |
| 313 | /** |
| 314 | * Draw the display buffer. |
| 315 | */ |
| 316 | @Override |
| 317 | public void draw() { |
| 318 | // Synchronize against the emulator so we don't stomp on its reader |
| 319 | // thread. |
| 320 | synchronized (emulator) { |
| 321 | |
| 322 | // Update the scroll bars |
| 323 | reflowData(); |
| 324 | |
| 325 | // Draw the box using my superclass |
| 326 | super.draw(); |
| 327 | |
| 328 | List<DisplayLine> scrollback = emulator.getScrollbackBuffer(); |
| 329 | List<DisplayLine> display = emulator.getDisplayBuffer(); |
| 330 | |
| 331 | // Put together the visible rows |
| 332 | int visibleHeight = getHeight() - 2; |
| 333 | int visibleBottom = scrollback.size() + display.size() |
| 334 | + getVerticalValue(); |
| 335 | assert (visibleBottom >= 0); |
| 336 | |
| 337 | List<DisplayLine> preceedingBlankLines = new ArrayList<DisplayLine>(); |
| 338 | int visibleTop = visibleBottom - visibleHeight; |
| 339 | if (visibleTop < 0) { |
| 340 | for (int i = visibleTop; i < 0; i++) { |
| 341 | preceedingBlankLines.add(emulator.getBlankDisplayLine()); |
| 342 | } |
| 343 | visibleTop = 0; |
| 344 | } |
| 345 | assert (visibleTop >= 0); |
| 346 | |
| 347 | List<DisplayLine> displayLines = new ArrayList<DisplayLine>(); |
| 348 | displayLines.addAll(scrollback); |
| 349 | displayLines.addAll(display); |
| 350 | |
| 351 | List<DisplayLine> visibleLines = new ArrayList<DisplayLine>(); |
| 352 | visibleLines.addAll(preceedingBlankLines); |
| 353 | visibleLines.addAll(displayLines.subList(visibleTop, |
| 354 | visibleBottom)); |
| 355 | |
| 356 | visibleHeight -= visibleLines.size(); |
| 357 | assert (visibleHeight >= 0); |
| 358 | |
| 359 | // Now draw the emulator screen |
| 360 | int row = 1; |
| 361 | for (DisplayLine line: visibleLines) { |
| 362 | int widthMax = emulator.getWidth(); |
| 363 | if (line.isDoubleWidth()) { |
| 364 | widthMax /= 2; |
| 365 | } |
| 366 | if (widthMax > getWidth() - 2) { |
| 367 | widthMax = getWidth() - 2; |
| 368 | } |
| 369 | for (int i = 0; i < widthMax; i++) { |
| 370 | Cell ch = line.charAt(i); |
| 371 | Cell newCell = new Cell(); |
| 372 | newCell.setTo(ch); |
| 373 | boolean reverse = line.isReverseColor() ^ ch.isReverse(); |
| 374 | newCell.setReverse(false); |
| 375 | if (reverse) { |
| 376 | if (ch.getForeColorRGB() < 0) { |
| 377 | newCell.setBackColor(ch.getForeColor()); |
| 378 | newCell.setBackColorRGB(-1); |
| 379 | } else { |
| 380 | newCell.setBackColorRGB(ch.getForeColorRGB()); |
| 381 | } |
| 382 | if (ch.getBackColorRGB() < 0) { |
| 383 | newCell.setForeColor(ch.getBackColor()); |
| 384 | newCell.setForeColorRGB(-1); |
| 385 | } else { |
| 386 | newCell.setForeColorRGB(ch.getBackColorRGB()); |
| 387 | } |
| 388 | } |
| 389 | if (line.isDoubleWidth()) { |
| 390 | putDoubleWidthCharXY(line, (i * 2) + 1, row, newCell); |
| 391 | } else { |
| 392 | putCharXY(i + 1, row, newCell); |
| 393 | } |
| 394 | } |
| 395 | row++; |
| 396 | if (row == getHeight() - 1) { |
| 397 | // Don't overwrite the box edge |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | CellAttributes background = new CellAttributes(); |
| 402 | // Fill in the blank lines on bottom |
| 403 | for (int i = 0; i < visibleHeight; i++) { |
| 404 | hLineXY(1, i + row, getWidth() - 2, ' ', background); |
| 405 | } |
| 406 | |
| 407 | } // synchronized (emulator) |
| 408 | |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Handle window close. |
| 413 | */ |
| 414 | @Override |
| 415 | public void onClose() { |
| 416 | emulator.close(); |
| 417 | if (shell != null) { |
| 418 | terminateShellChildProcess(); |
| 419 | shell.destroy(); |
| 420 | shell = null; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Handle window/screen resize events. |
| 426 | * |
| 427 | * @param resize resize event |
| 428 | */ |
| 429 | @Override |
| 430 | public void onResize(final TResizeEvent resize) { |
| 431 | |
| 432 | // Synchronize against the emulator so we don't stomp on its reader |
| 433 | // thread. |
| 434 | synchronized (emulator) { |
| 435 | |
| 436 | if (resize.getType() == TResizeEvent.Type.WIDGET) { |
| 437 | // Resize the scroll bars |
| 438 | reflowData(); |
| 439 | placeScrollbars(); |
| 440 | |
| 441 | // Get out of scrollback |
| 442 | setVerticalValue(0); |
| 443 | |
| 444 | if (ptypipe) { |
| 445 | emulator.setWidth(getWidth() - 2); |
| 446 | emulator.setHeight(getHeight() - 2); |
| 447 | |
| 448 | emulator.writeRemote("\033[8;" + (getHeight() - 2) + ";" + |
| 449 | (getWidth() - 2) + "t"); |
| 450 | } |
| 451 | } |
| 452 | return; |
| 453 | |
| 454 | } // synchronized (emulator) |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Resize scrollbars for a new width/height. |
| 459 | */ |
| 460 | @Override |
| 461 | public void reflowData() { |
| 462 | |
| 463 | // Synchronize against the emulator so we don't stomp on its reader |
| 464 | // thread. |
| 465 | synchronized (emulator) { |
| 466 | |
| 467 | // Pull cursor information |
| 468 | readEmulatorState(); |
| 469 | |
| 470 | // Vertical scrollbar |
| 471 | setTopValue(getHeight() - 2 |
| 472 | - (emulator.getScrollbackBuffer().size() |
| 473 | + emulator.getDisplayBuffer().size())); |
| 474 | setVerticalBigChange(getHeight() - 2); |
| 475 | |
| 476 | } // synchronized (emulator) |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Handle keystrokes. |
| 481 | * |
| 482 | * @param keypress keystroke event |
| 483 | */ |
| 484 | @Override |
| 485 | public void onKeypress(final TKeypressEvent keypress) { |
| 486 | |
| 487 | // Scrollback up/down |
| 488 | if (keypress.equals(kbShiftPgUp) |
| 489 | || keypress.equals(kbCtrlPgUp) |
| 490 | || keypress.equals(kbAltPgUp) |
| 491 | ) { |
| 492 | bigVerticalDecrement(); |
| 493 | return; |
| 494 | } |
| 495 | if (keypress.equals(kbShiftPgDn) |
| 496 | || keypress.equals(kbCtrlPgDn) |
| 497 | || keypress.equals(kbAltPgDn) |
| 498 | ) { |
| 499 | bigVerticalIncrement(); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | // Synchronize against the emulator so we don't stomp on its reader |
| 504 | // thread. |
| 505 | synchronized (emulator) { |
| 506 | if (emulator.isReading()) { |
| 507 | // Get out of scrollback |
| 508 | setVerticalValue(0); |
| 509 | emulator.keypress(keypress.getKey()); |
| 510 | |
| 511 | // UGLY HACK TIME! cmd.exe needs CRLF, not just CR, so if |
| 512 | // this is kBEnter then also send kbCtrlJ. |
| 513 | if (System.getProperty("os.name").startsWith("Windows")) { |
| 514 | if (keypress.equals(kbEnter)) { |
| 515 | emulator.keypress(kbCtrlJ); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | readEmulatorState(); |
| 520 | return; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Process is closed, honor "normal" TUI keystrokes |
| 525 | super.onKeypress(keypress); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Handle mouse press events. |
| 530 | * |
| 531 | * @param mouse mouse button press event |
| 532 | */ |
| 533 | @Override |
| 534 | public void onMouseDown(final TMouseEvent mouse) { |
| 535 | if (inWindowMove || inWindowResize) { |
| 536 | // TWindow needs to deal with this. |
| 537 | super.onMouseDown(mouse); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | if (mouse.isMouseWheelUp()) { |
| 542 | verticalDecrement(); |
| 543 | return; |
| 544 | } |
| 545 | if (mouse.isMouseWheelDown()) { |
| 546 | verticalIncrement(); |
| 547 | return; |
| 548 | } |
| 549 | if (mouseOnEmulator(mouse)) { |
| 550 | synchronized (emulator) { |
| 551 | mouse.setX(mouse.getX() - 1); |
| 552 | mouse.setY(mouse.getY() - 1); |
| 553 | emulator.mouse(mouse); |
| 554 | readEmulatorState(); |
| 555 | return; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // Emulator didn't consume it, pass it on |
| 560 | super.onMouseDown(mouse); |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Handle mouse release events. |
| 565 | * |
| 566 | * @param mouse mouse button release event |
| 567 | */ |
| 568 | @Override |
| 569 | public void onMouseUp(final TMouseEvent mouse) { |
| 570 | if (inWindowMove || inWindowResize) { |
| 571 | // TWindow needs to deal with this. |
| 572 | super.onMouseUp(mouse); |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | if (mouseOnEmulator(mouse)) { |
| 577 | synchronized (emulator) { |
| 578 | mouse.setX(mouse.getX() - 1); |
| 579 | mouse.setY(mouse.getY() - 1); |
| 580 | emulator.mouse(mouse); |
| 581 | readEmulatorState(); |
| 582 | return; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // Emulator didn't consume it, pass it on |
| 587 | super.onMouseUp(mouse); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Handle mouse motion events. |
| 592 | * |
| 593 | * @param mouse mouse motion event |
| 594 | */ |
| 595 | @Override |
| 596 | public void onMouseMotion(final TMouseEvent mouse) { |
| 597 | if (inWindowMove || inWindowResize) { |
| 598 | // TWindow needs to deal with this. |
| 599 | super.onMouseMotion(mouse); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | if (mouseOnEmulator(mouse)) { |
| 604 | synchronized (emulator) { |
| 605 | mouse.setX(mouse.getX() - 1); |
| 606 | mouse.setY(mouse.getY() - 1); |
| 607 | emulator.mouse(mouse); |
| 608 | readEmulatorState(); |
| 609 | return; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | // Emulator didn't consume it, pass it on |
| 614 | super.onMouseMotion(mouse); |
| 615 | } |
| 616 | |
| 617 | // ------------------------------------------------------------------------ |
| 618 | // TTerminalWindow -------------------------------------------------------- |
| 619 | // ------------------------------------------------------------------------ |
| 620 | |
| 621 | /** |
| 622 | * Claim the keystrokes the emulator will need. |
| 623 | */ |
| 624 | private void addShortcutKeys() { |
| 625 | addShortcutKeypress(kbCtrlA); |
| 626 | addShortcutKeypress(kbCtrlB); |
| 627 | addShortcutKeypress(kbCtrlC); |
| 628 | addShortcutKeypress(kbCtrlD); |
| 629 | addShortcutKeypress(kbCtrlE); |
| 630 | addShortcutKeypress(kbCtrlF); |
| 631 | addShortcutKeypress(kbCtrlG); |
| 632 | addShortcutKeypress(kbCtrlH); |
| 633 | addShortcutKeypress(kbCtrlU); |
| 634 | addShortcutKeypress(kbCtrlJ); |
| 635 | addShortcutKeypress(kbCtrlK); |
| 636 | addShortcutKeypress(kbCtrlL); |
| 637 | addShortcutKeypress(kbCtrlM); |
| 638 | addShortcutKeypress(kbCtrlN); |
| 639 | addShortcutKeypress(kbCtrlO); |
| 640 | addShortcutKeypress(kbCtrlP); |
| 641 | addShortcutKeypress(kbCtrlQ); |
| 642 | addShortcutKeypress(kbCtrlR); |
| 643 | addShortcutKeypress(kbCtrlS); |
| 644 | addShortcutKeypress(kbCtrlT); |
| 645 | addShortcutKeypress(kbCtrlU); |
| 646 | addShortcutKeypress(kbCtrlV); |
| 647 | addShortcutKeypress(kbCtrlW); |
| 648 | addShortcutKeypress(kbCtrlX); |
| 649 | addShortcutKeypress(kbCtrlY); |
| 650 | addShortcutKeypress(kbCtrlZ); |
| 651 | addShortcutKeypress(kbF1); |
| 652 | addShortcutKeypress(kbF2); |
| 653 | addShortcutKeypress(kbF3); |
| 654 | addShortcutKeypress(kbF4); |
| 655 | addShortcutKeypress(kbF5); |
| 656 | addShortcutKeypress(kbF6); |
| 657 | addShortcutKeypress(kbF7); |
| 658 | addShortcutKeypress(kbF8); |
| 659 | addShortcutKeypress(kbF9); |
| 660 | addShortcutKeypress(kbF10); |
| 661 | addShortcutKeypress(kbF11); |
| 662 | addShortcutKeypress(kbF12); |
| 663 | addShortcutKeypress(kbAltA); |
| 664 | addShortcutKeypress(kbAltB); |
| 665 | addShortcutKeypress(kbAltC); |
| 666 | addShortcutKeypress(kbAltD); |
| 667 | addShortcutKeypress(kbAltE); |
| 668 | addShortcutKeypress(kbAltF); |
| 669 | addShortcutKeypress(kbAltG); |
| 670 | addShortcutKeypress(kbAltH); |
| 671 | addShortcutKeypress(kbAltU); |
| 672 | addShortcutKeypress(kbAltJ); |
| 673 | addShortcutKeypress(kbAltK); |
| 674 | addShortcutKeypress(kbAltL); |
| 675 | addShortcutKeypress(kbAltM); |
| 676 | addShortcutKeypress(kbAltN); |
| 677 | addShortcutKeypress(kbAltO); |
| 678 | addShortcutKeypress(kbAltP); |
| 679 | addShortcutKeypress(kbAltQ); |
| 680 | addShortcutKeypress(kbAltR); |
| 681 | addShortcutKeypress(kbAltS); |
| 682 | addShortcutKeypress(kbAltT); |
| 683 | addShortcutKeypress(kbAltU); |
| 684 | addShortcutKeypress(kbAltV); |
| 685 | addShortcutKeypress(kbAltW); |
| 686 | addShortcutKeypress(kbAltX); |
| 687 | addShortcutKeypress(kbAltY); |
| 688 | addShortcutKeypress(kbAltZ); |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * Convert a string array to a whitespace-separated string. |
| 693 | * |
| 694 | * @param array the string array |
| 695 | * @return a single string |
| 696 | */ |
| 697 | private String stringArrayToString(final String [] array) { |
| 698 | StringBuilder sb = new StringBuilder(array[0].length()); |
| 699 | for (int i = 0; i < array.length; i++) { |
| 700 | sb.append(array[i]); |
| 701 | if (i < array.length - 1) { |
| 702 | sb.append(' '); |
| 703 | } |
| 704 | } |
| 705 | return sb.toString(); |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Spawn the shell. |
| 710 | * |
| 711 | * @param command the command line to execute |
| 712 | */ |
| 713 | private void spawnShell(final String [] command) { |
| 714 | |
| 715 | /* |
| 716 | System.err.printf("spawnShell(): '%s'\n", |
| 717 | stringArrayToString(command)); |
| 718 | */ |
| 719 | |
| 720 | vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2); |
| 721 | setBottomValue(0); |
| 722 | |
| 723 | // Assume XTERM |
| 724 | ECMA48.DeviceType deviceType = ECMA48.DeviceType.XTERM; |
| 725 | |
| 726 | try { |
| 727 | ProcessBuilder pb = new ProcessBuilder(command); |
| 728 | Map<String, String> env = pb.environment(); |
| 729 | env.put("TERM", ECMA48.deviceTypeTerm(deviceType)); |
| 730 | env.put("LANG", ECMA48.deviceTypeLang(deviceType, "en")); |
| 731 | env.put("COLUMNS", "80"); |
| 732 | env.put("LINES", "24"); |
| 733 | pb.redirectErrorStream(true); |
| 734 | shell = pb.start(); |
| 735 | emulator = new ECMA48(deviceType, shell.getInputStream(), |
| 736 | shell.getOutputStream(), this); |
| 737 | } catch (IOException e) { |
| 738 | messageBox(i18n.getString("errorLaunchingShellTitle"), |
| 739 | MessageFormat.format(i18n.getString("errorLaunchingShellText"), |
| 740 | e.getMessage())); |
| 741 | } |
| 742 | |
| 743 | // Setup the scroll bars |
| 744 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(), |
| 745 | getHeight())); |
| 746 | |
| 747 | // Claim the keystrokes the emulator will need. |
| 748 | addShortcutKeys(); |
| 749 | |
| 750 | // Add shortcut text |
| 751 | newStatusBar(i18n.getString("statusBarRunning")); |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Terminate the child of the 'script' process used on POSIX. This may |
| 756 | * or may not work. |
| 757 | */ |
| 758 | private void terminateShellChildProcess() { |
| 759 | int pid = -1; |
| 760 | if (shell.getClass().getName().equals("java.lang.UNIXProcess")) { |
| 761 | /* get the PID on unix/linux systems */ |
| 762 | try { |
| 763 | Field field = shell.getClass().getDeclaredField("pid"); |
| 764 | field.setAccessible(true); |
| 765 | pid = field.getInt(shell); |
| 766 | } catch (Throwable e) { |
| 767 | // SQUASH, this didn't work. Just bail out quietly. |
| 768 | return; |
| 769 | } |
| 770 | } |
| 771 | if (pid != -1) { |
| 772 | // shell.destroy() works successfully at killing this side of |
| 773 | // 'script'. But we need to make sure the other side (child |
| 774 | // process) is also killed. |
| 775 | String [] cmdKillIt = { |
| 776 | "pkill", "-P", Integer.toString(pid) |
| 777 | }; |
| 778 | try { |
| 779 | Runtime.getRuntime().exec(cmdKillIt); |
| 780 | } catch (Throwable e) { |
| 781 | // SQUASH, this didn't work. Just bail out quietly. |
| 782 | return; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * Called by emulator when fresh data has come in. |
| 789 | */ |
| 790 | public void displayChanged() { |
| 791 | getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT)); |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * Function to call to obtain the display width. |
| 796 | * |
| 797 | * @return the number of columns in the display |
| 798 | */ |
| 799 | public int getDisplayWidth() { |
| 800 | if (ptypipe) { |
| 801 | return getWidth() - 2; |
| 802 | } |
| 803 | return 80; |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * Function to call to obtain the display height. |
| 808 | * |
| 809 | * @return the number of rows in the display |
| 810 | */ |
| 811 | public int getDisplayHeight() { |
| 812 | if (ptypipe) { |
| 813 | return getHeight() - 2; |
| 814 | } |
| 815 | return 24; |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * Hook for subclasses to be notified of the shell termination. |
| 820 | */ |
| 821 | public void onShellExit() { |
| 822 | if (closeOnExit) { |
| 823 | close(); |
| 824 | } |
| 825 | getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT)); |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Copy out variables from the emulator that TTerminal has to expose on |
| 830 | * screen. |
| 831 | */ |
| 832 | private void readEmulatorState() { |
| 833 | // Synchronize against the emulator so we don't stomp on its reader |
| 834 | // thread. |
| 835 | synchronized (emulator) { |
| 836 | setHiddenMouse(emulator.hasHiddenMousePointer()); |
| 837 | |
| 838 | setCursorX(emulator.getCursorX() + 1); |
| 839 | setCursorY(emulator.getCursorY() + 1 |
| 840 | + (getHeight() - 2 - emulator.getHeight()) |
| 841 | - getVerticalValue()); |
| 842 | setCursorVisible(emulator.isCursorVisible()); |
| 843 | if (getCursorX() > getWidth() - 2) { |
| 844 | setCursorVisible(false); |
| 845 | } |
| 846 | if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) { |
| 847 | setCursorVisible(false); |
| 848 | } |
| 849 | if (emulator.getScreenTitle().length() > 0) { |
| 850 | // Only update the title if the shell is still alive |
| 851 | if (shell != null) { |
| 852 | setTitle(emulator.getScreenTitle()); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | // Check to see if the shell has died. |
| 857 | if (!emulator.isReading() && (shell != null)) { |
| 858 | try { |
| 859 | int rc = shell.exitValue(); |
| 860 | // The emulator exited on its own, all is fine |
| 861 | setTitle(MessageFormat.format(i18n. |
| 862 | getString("windowTitleCompleted"), getTitle(), rc)); |
| 863 | shell = null; |
| 864 | emulator.close(); |
| 865 | clearShortcutKeypresses(); |
| 866 | statusBar.setText(MessageFormat.format(i18n. |
| 867 | getString("statusBarCompleted"), rc)); |
| 868 | onShellExit(); |
| 869 | } catch (IllegalThreadStateException e) { |
| 870 | // The emulator thread has exited, but the shell Process |
| 871 | // hasn't figured that out yet. Do nothing, we will see |
| 872 | // this in a future tick. |
| 873 | } |
| 874 | } else if (emulator.isReading() && (shell != null)) { |
| 875 | // The shell might be dead, let's check |
| 876 | try { |
| 877 | int rc = shell.exitValue(); |
| 878 | // If we got here, the shell died. |
| 879 | setTitle(MessageFormat.format(i18n. |
| 880 | getString("windowTitleCompleted"), getTitle(), rc)); |
| 881 | shell = null; |
| 882 | emulator.close(); |
| 883 | clearShortcutKeypresses(); |
| 884 | statusBar.setText(MessageFormat.format(i18n. |
| 885 | getString("statusBarCompleted"), rc)); |
| 886 | onShellExit(); |
| 887 | } catch (IllegalThreadStateException e) { |
| 888 | // The shell is still running, do nothing. |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | } // synchronized (emulator) |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Check if a mouse press/release/motion event coordinate is over the |
| 897 | * emulator. |
| 898 | * |
| 899 | * @param mouse a mouse-based event |
| 900 | * @return whether or not the mouse is on the emulator |
| 901 | */ |
| 902 | private boolean mouseOnEmulator(final TMouseEvent mouse) { |
| 903 | |
| 904 | synchronized (emulator) { |
| 905 | if (!emulator.isReading()) { |
| 906 | return false; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1) |
| 911 | && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1) |
| 912 | && (mouse.getAbsoluteY() >= getAbsoluteY() + 1) |
| 913 | && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1) |
| 914 | ) { |
| 915 | return true; |
| 916 | } |
| 917 | return false; |
| 918 | } |
| 919 | |
| 920 | /** |
| 921 | * Draw glyphs for a double-width or double-height VT100 cell to two |
| 922 | * screen cells. |
| 923 | * |
| 924 | * @param line the line this VT100 cell is in |
| 925 | * @param x the X position to draw the left half to |
| 926 | * @param y the Y position to draw to |
| 927 | * @param cell the cell to draw |
| 928 | */ |
| 929 | private void putDoubleWidthCharXY(final DisplayLine line, final int x, |
| 930 | final int y, final Cell cell) { |
| 931 | |
| 932 | int textWidth = 16; |
| 933 | int textHeight = 20; |
| 934 | |
| 935 | if (getScreen() instanceof SwingTerminal) { |
| 936 | SwingTerminal terminal = (SwingTerminal) getScreen(); |
| 937 | |
| 938 | textWidth = terminal.getTextWidth(); |
| 939 | textHeight = terminal.getTextHeight(); |
| 940 | } else if (getScreen() instanceof ECMA48Terminal) { |
| 941 | ECMA48Terminal terminal = (ECMA48Terminal) getScreen(); |
| 942 | |
| 943 | textWidth = terminal.getTextWidth(); |
| 944 | textHeight = terminal.getTextHeight(); |
| 945 | } else { |
| 946 | // We don't know how to dray glyphs to this screen, draw them as |
| 947 | // text and bail out. |
| 948 | putCharXY(x, y, cell); |
| 949 | putCharXY(x + 1, y, ' ', cell); |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | if ((textWidth != lastTextWidth) || (textHeight != lastTextHeight)) { |
| 954 | // Screen size has changed, reset all fonts. |
| 955 | setupFonts(textHeight); |
| 956 | lastTextWidth = textWidth; |
| 957 | lastTextHeight = textHeight; |
| 958 | } |
| 959 | assert (doubleFont != null); |
| 960 | |
| 961 | BufferedImage image = null; |
| 962 | |
| 963 | image = glyphCache.get(cell); |
| 964 | if (image == null) { |
| 965 | // Generate glyph and draw it to an image. |
| 966 | image = new BufferedImage(textWidth * 2, textHeight * 2, |
| 967 | BufferedImage.TYPE_INT_ARGB); |
| 968 | Graphics2D gr2 = image.createGraphics(); |
| 969 | gr2.setFont(doubleFont); |
| 970 | |
| 971 | // Draw the background rectangle, then the foreground character. |
| 972 | if (getScreen() instanceof ECMA48Terminal) { |
| 973 | // BUG: the background color is coming in the same as the |
| 974 | // foreground color. For now, don't draw it. |
| 975 | } else { |
| 976 | gr2.setColor(SwingTerminal.attrToBackgroundColor(cell)); |
| 977 | gr2.fillRect(0, 0, image.getWidth(), image.getHeight()); |
| 978 | } |
| 979 | gr2.setColor(SwingTerminal.attrToForegroundColor(cell)); |
| 980 | char [] chars = new char[1]; |
| 981 | chars[0] = cell.getChar(); |
| 982 | gr2.drawChars(chars, 0, 1, doubleTextAdjustX, |
| 983 | (textHeight * 2) - doubleMaxDescent + doubleTextAdjustY); |
| 984 | |
| 985 | if (cell.isUnderline() && (line.getDoubleHeight() != 1)) { |
| 986 | gr2.fillRect(0, textHeight - 2, textWidth, 2); |
| 987 | } |
| 988 | gr2.dispose(); |
| 989 | |
| 990 | // Now save this generated image, using a new key that will not |
| 991 | // be mutated by invertCell(). |
| 992 | Cell key = new Cell(); |
| 993 | key.setTo(cell); |
| 994 | glyphCache.put(key, image); |
| 995 | } |
| 996 | |
| 997 | // Now that we have the double-wide glyph drawn, copy the right |
| 998 | // pieces of it to the cells. |
| 999 | Cell left = new Cell(); |
| 1000 | Cell right = new Cell(); |
| 1001 | left.setTo(cell); |
| 1002 | right.setTo(cell); |
| 1003 | right.setChar(' '); |
| 1004 | BufferedImage leftImage = null; |
| 1005 | BufferedImage rightImage = null; |
| 1006 | switch (line.getDoubleHeight()) { |
| 1007 | case 1: |
| 1008 | // Top half double height |
| 1009 | leftImage = image.getSubimage(0, 0, textWidth, textHeight); |
| 1010 | rightImage = image.getSubimage(textWidth, 0, textWidth, textHeight); |
| 1011 | break; |
| 1012 | case 2: |
| 1013 | // Bottom half double height |
| 1014 | leftImage = image.getSubimage(0, textHeight, textWidth, textHeight); |
| 1015 | rightImage = image.getSubimage(textWidth, textHeight, |
| 1016 | textWidth, textHeight); |
| 1017 | break; |
| 1018 | default: |
| 1019 | // Either single height double-width, or error fallback |
| 1020 | BufferedImage wideImage = new BufferedImage(textWidth * 2, |
| 1021 | textHeight, BufferedImage.TYPE_INT_ARGB); |
| 1022 | Graphics2D grWide = wideImage.createGraphics(); |
| 1023 | grWide.drawImage(image, 0, 0, wideImage.getWidth(), |
| 1024 | wideImage.getHeight(), null); |
| 1025 | grWide.dispose(); |
| 1026 | leftImage = wideImage.getSubimage(0, 0, textWidth, textHeight); |
| 1027 | rightImage = wideImage.getSubimage(textWidth, 0, textWidth, |
| 1028 | textHeight); |
| 1029 | break; |
| 1030 | } |
| 1031 | left.setImage(leftImage); |
| 1032 | right.setImage(rightImage); |
| 1033 | putCharXY(x, y, left); |
| 1034 | putCharXY(x + 1, y, right); |
| 1035 | } |
| 1036 | |
| 1037 | /** |
| 1038 | * Set up the single and double-width fonts. |
| 1039 | * |
| 1040 | * @param fontSize the size of font to request for the single-width font. |
| 1041 | * The double-width font will be 2x this value. |
| 1042 | */ |
| 1043 | private void setupFonts(final int fontSize) { |
| 1044 | try { |
| 1045 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 1046 | InputStream in = loader.getResourceAsStream(SwingTerminal.FONTFILE); |
| 1047 | Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in); |
| 1048 | Font terminusDouble = terminusRoot.deriveFont(Font.PLAIN, |
| 1049 | fontSize * 2); |
| 1050 | doubleFont = terminusDouble; |
| 1051 | } catch (java.awt.FontFormatException e) { |
| 1052 | new TExceptionDialog(getApplication(), e); |
| 1053 | doubleFont = new Font(Font.MONOSPACED, Font.PLAIN, fontSize * 2); |
| 1054 | } catch (java.io.IOException e) { |
| 1055 | new TExceptionDialog(getApplication(), e); |
| 1056 | doubleFont = new Font(Font.MONOSPACED, Font.PLAIN, fontSize * 2); |
| 1057 | } |
| 1058 | |
| 1059 | // Get font descent. |
| 1060 | BufferedImage image = new BufferedImage(fontSize * 10, fontSize * 10, |
| 1061 | BufferedImage.TYPE_INT_ARGB); |
| 1062 | Graphics2D gr = image.createGraphics(); |
| 1063 | gr.setFont(doubleFont); |
| 1064 | FontMetrics fm = gr.getFontMetrics(); |
| 1065 | doubleMaxDescent = fm.getMaxDescent(); |
| 1066 | |
| 1067 | gr.dispose(); |
| 1068 | |
| 1069 | // (Re)create the glyph cache. |
| 1070 | glyphCache = new HashMap<Cell, BufferedImage>(); |
| 1071 | } |
| 1072 | |
| 1073 | } |