+ // ------------------------------------------------------------------------
+ // TTerminalWindow --------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Claim the keystrokes the emulator will need.
+ */
+ private void addShortcutKeys() {
+ addShortcutKeypress(kbCtrlA);
+ addShortcutKeypress(kbCtrlB);
+ addShortcutKeypress(kbCtrlC);
+ addShortcutKeypress(kbCtrlD);
+ addShortcutKeypress(kbCtrlE);
+ addShortcutKeypress(kbCtrlF);
+ addShortcutKeypress(kbCtrlG);
+ addShortcutKeypress(kbCtrlH);
+ addShortcutKeypress(kbCtrlU);
+ addShortcutKeypress(kbCtrlJ);
+ addShortcutKeypress(kbCtrlK);
+ addShortcutKeypress(kbCtrlL);
+ addShortcutKeypress(kbCtrlM);
+ addShortcutKeypress(kbCtrlN);
+ addShortcutKeypress(kbCtrlO);
+ addShortcutKeypress(kbCtrlP);
+ addShortcutKeypress(kbCtrlQ);
+ addShortcutKeypress(kbCtrlR);
+ addShortcutKeypress(kbCtrlS);
+ addShortcutKeypress(kbCtrlT);
+ addShortcutKeypress(kbCtrlU);
+ addShortcutKeypress(kbCtrlV);
+ addShortcutKeypress(kbCtrlW);
+ addShortcutKeypress(kbCtrlX);
+ addShortcutKeypress(kbCtrlY);
+ addShortcutKeypress(kbCtrlZ);
+ addShortcutKeypress(kbF1);
+ addShortcutKeypress(kbF2);
+ addShortcutKeypress(kbF3);
+ addShortcutKeypress(kbF4);
+ addShortcutKeypress(kbF5);
+ addShortcutKeypress(kbF6);
+ addShortcutKeypress(kbF7);
+ addShortcutKeypress(kbF8);
+ addShortcutKeypress(kbF9);
+ addShortcutKeypress(kbF10);
+ addShortcutKeypress(kbF11);
+ addShortcutKeypress(kbF12);
+ addShortcutKeypress(kbAltA);
+ addShortcutKeypress(kbAltB);
+ addShortcutKeypress(kbAltC);
+ addShortcutKeypress(kbAltD);
+ addShortcutKeypress(kbAltE);
+ addShortcutKeypress(kbAltF);
+ addShortcutKeypress(kbAltG);
+ addShortcutKeypress(kbAltH);
+ addShortcutKeypress(kbAltU);
+ addShortcutKeypress(kbAltJ);
+ addShortcutKeypress(kbAltK);
+ addShortcutKeypress(kbAltL);
+ addShortcutKeypress(kbAltM);
+ addShortcutKeypress(kbAltN);
+ addShortcutKeypress(kbAltO);
+ addShortcutKeypress(kbAltP);
+ addShortcutKeypress(kbAltQ);
+ addShortcutKeypress(kbAltR);
+ addShortcutKeypress(kbAltS);
+ addShortcutKeypress(kbAltT);
+ addShortcutKeypress(kbAltU);
+ addShortcutKeypress(kbAltV);
+ addShortcutKeypress(kbAltW);
+ addShortcutKeypress(kbAltX);
+ addShortcutKeypress(kbAltY);
+ addShortcutKeypress(kbAltZ);
+ }
+
+ /**
+ * Convert a string array to a whitespace-separated string.
+ *
+ * @param array the string array
+ * @return a single string
+ */
+ private String stringArrayToString(final String [] array) {
+ StringBuilder sb = new StringBuilder(array[0].length());
+ for (int i = 0; i < array.length; i++) {
+ sb.append(array[i]);
+ if (i < array.length - 1) {
+ sb.append(' ');
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Spawn the shell.
+ *
+ * @param command the command line to execute
+ */
+ private void spawnShell(final String [] command) {
+
+ /*
+ System.err.printf("spawnShell(): '%s'\n",
+ stringArrayToString(command));
+ */
+
+ vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
+ setBottomValue(0);
+
+ // Assume XTERM
+ ECMA48.DeviceType deviceType = ECMA48.DeviceType.XTERM;
+
+ try {
+ ProcessBuilder pb = new ProcessBuilder(command);
+ Map<String, String> env = pb.environment();
+ env.put("TERM", ECMA48.deviceTypeTerm(deviceType));
+ env.put("LANG", ECMA48.deviceTypeLang(deviceType, "en"));
+ env.put("COLUMNS", "80");
+ env.put("LINES", "24");
+ pb.redirectErrorStream(true);
+ shell = pb.start();
+ emulator = new ECMA48(deviceType, shell.getInputStream(),
+ shell.getOutputStream(), this);
+ } catch (IOException e) {
+ messageBox(i18n.getString("errorLaunchingShellTitle"),
+ MessageFormat.format(i18n.getString("errorLaunchingShellText"),
+ e.getMessage()));
+ }
+
+ // Setup the scroll bars
+ onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
+ getHeight()));
+
+ // Claim the keystrokes the emulator will need.
+ addShortcutKeys();
+
+ // Add shortcut text
+ newStatusBar(i18n.getString("statusBarRunning"));
+
+ // Pass the correct text cell width/height to the emulator
+ emulator.setTextWidth(getScreen().getTextWidth());
+ emulator.setTextHeight(getScreen().getTextHeight());
+ }
+
+ /**
+ * Terminate the child of the 'script' process used on POSIX. This may
+ * or may not work.
+ */
+ private void terminateShellChildProcess() {
+ int pid = -1;
+ if (shell.getClass().getName().equals("java.lang.UNIXProcess")) {
+ /* get the PID on unix/linux systems */
+ try {
+ Field field = shell.getClass().getDeclaredField("pid");
+ field.setAccessible(true);
+ pid = field.getInt(shell);
+ } catch (Throwable e) {
+ // SQUASH, this didn't work. Just bail out quietly.
+ return;
+ }
+ }
+ if (pid != -1) {
+ // shell.destroy() works successfully at killing this side of
+ // 'script'. But we need to make sure the other side (child
+ // process) is also killed.
+ String [] cmdKillIt = {
+ "pkill", "-P", Integer.toString(pid)
+ };
+ try {
+ Runtime.getRuntime().exec(cmdKillIt);
+ } catch (Throwable e) {
+ // SQUASH, this didn't work. Just bail out quietly.
+ return;
+ }
+ }
+ }
+
+ /**
+ * Hook for subclasses to be notified of the shell termination.
+ */
+ public void onShellExit() {
+ if (closeOnExit) {
+ close();
+ }
+ getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
+ }
+
+ /**
+ * Copy out variables from the emulator that TTerminal has to expose on
+ * screen.
+ */
+ private void readEmulatorState() {
+ // Synchronize against the emulator so we don't stomp on its reader
+ // thread.
+ synchronized (emulator) {
+ setHiddenMouse(emulator.hasHiddenMousePointer());
+
+ setCursorX(emulator.getCursorX() + 1);
+ setCursorY(emulator.getCursorY() + 1
+ + (getHeight() - 2 - emulator.getHeight())
+ - getVerticalValue());
+ setCursorVisible(emulator.isCursorVisible());
+ if (getCursorX() > getWidth() - 2) {
+ setCursorVisible(false);
+ }
+ if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
+ setCursorVisible(false);
+ }
+ if (emulator.getScreenTitle().length() > 0) {
+ // Only update the title if the shell is still alive
+ if (shell != null) {
+ setTitle(emulator.getScreenTitle());
+ }
+ }
+
+ // Check to see if the shell has died.
+ if (!emulator.isReading() && (shell != null)) {
+ try {
+ int rc = shell.exitValue();
+ // The emulator exited on its own, all is fine
+ setTitle(MessageFormat.format(i18n.
+ getString("windowTitleCompleted"), getTitle(), rc));
+ shell = null;
+ emulator.close();
+ clearShortcutKeypresses();
+ statusBar.setText(MessageFormat.format(i18n.
+ getString("statusBarCompleted"), rc));
+ onShellExit();
+ } catch (IllegalThreadStateException e) {
+ // The emulator thread has exited, but the shell Process
+ // hasn't figured that out yet. Do nothing, we will see
+ // this in a future tick.
+ }
+ } else if (emulator.isReading() && (shell != null)) {
+ // The shell might be dead, let's check
+ try {
+ int rc = shell.exitValue();
+ // If we got here, the shell died.
+ setTitle(MessageFormat.format(i18n.
+ getString("windowTitleCompleted"), getTitle(), rc));
+ shell = null;
+ emulator.close();
+ clearShortcutKeypresses();
+ statusBar.setText(MessageFormat.format(i18n.
+ getString("statusBarCompleted"), rc));
+ onShellExit();
+ } catch (IllegalThreadStateException e) {
+ // The shell is still running, do nothing.
+ }
+ }
+
+ } // synchronized (emulator)
+ }
+
+ /**
+ * Check if a mouse press/release/motion event coordinate is over the
+ * emulator.
+ *
+ * @param mouse a mouse-based event
+ * @return whether or not the mouse is on the emulator
+ */
+ private boolean mouseOnEmulator(final TMouseEvent mouse) {
+
+ if (!emulator.isReading()) {
+ return false;
+ }
+
+ if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
+ && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
+ && (mouse.getAbsoluteY() >= getAbsoluteY() + 1)
+ && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
+ ) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Copy a display buffer.
+ *
+ * @param buffer the buffer to copy
+ * @return a deep copy of the buffer's data
+ */
+ private List<DisplayLine> copyBuffer(final List<DisplayLine> buffer) {
+ ArrayList<DisplayLine> result = new ArrayList<DisplayLine>(buffer.size());
+ for (DisplayLine line: buffer) {
+ result.add(new DisplayLine(line));
+ }
+ return result;
+ }
+
+ /**
+ * Draw glyphs for a double-width or double-height VT100 cell to two
+ * screen cells.
+ *
+ * @param line the line this VT100 cell is in
+ * @param x the X position to draw the left half to
+ * @param y the Y position to draw to
+ * @param cell the cell to draw
+ */
+ private void putDoubleWidthCharXY(final DisplayLine line, final int x,
+ final int y, final Cell cell) {
+
+ int textWidth = getScreen().getTextWidth();
+ int textHeight = getScreen().getTextHeight();
+ boolean cursorBlinkVisible = true;
+
+ if (getScreen() instanceof SwingTerminal) {
+ SwingTerminal terminal = (SwingTerminal) getScreen();
+ cursorBlinkVisible = terminal.getCursorBlinkVisible();
+ } else if (getScreen() instanceof ECMA48Terminal) {
+ ECMA48Terminal terminal = (ECMA48Terminal) getScreen();
+
+ if (!terminal.hasSixel()) {
+ // The backend does not have sixel support, draw this as text
+ // and bail out.
+ putCharXY(x, y, cell);
+ putCharXY(x + 1, y, ' ', cell);
+ return;
+ }
+ cursorBlinkVisible = blinkState;
+ } else {
+ // We don't know how to dray glyphs to this screen, draw them as
+ // text and bail out.
+ putCharXY(x, y, cell);
+ putCharXY(x + 1, y, ' ', cell);
+ return;
+ }
+
+ if ((textWidth != lastTextWidth) || (textHeight != lastTextHeight)) {
+ // Screen size has changed, reset the font.
+ setupFont(textHeight);
+ lastTextWidth = textWidth;
+ lastTextHeight = textHeight;
+ }
+ assert (doubleFont != null);
+
+ BufferedImage image;
+ if (line.getDoubleHeight() == 1) {
+ // Double-height top half: don't draw the underline.
+ Cell newCell = new Cell(cell);
+ newCell.setUnderline(false);
+ image = doubleFont.getImage(newCell, textWidth * 2, textHeight * 2,
+ cursorBlinkVisible);
+ } else {
+ image = doubleFont.getImage(cell, textWidth * 2, textHeight * 2,
+ cursorBlinkVisible);
+ }
+
+ // Now that we have the double-wide glyph drawn, copy the right
+ // pieces of it to the cells.
+ Cell left = new Cell(cell);
+ Cell right = new Cell(cell);
+ right.setChar(' ');
+ BufferedImage leftImage = null;
+ BufferedImage rightImage = null;
+ /*
+ System.err.println("image " + image + " textWidth " + textWidth +
+ " textHeight " + textHeight);
+ */
+
+ switch (line.getDoubleHeight()) {
+ case 1:
+ // Top half double height
+ leftImage = image.getSubimage(0, 0, textWidth, textHeight);
+ rightImage = image.getSubimage(textWidth, 0, textWidth, textHeight);
+ break;
+ case 2:
+ // Bottom half double height
+ leftImage = image.getSubimage(0, textHeight, textWidth, textHeight);
+ rightImage = image.getSubimage(textWidth, textHeight,
+ textWidth, textHeight);
+ break;
+ default:
+ // Either single height double-width, or error fallback
+ BufferedImage wideImage = new BufferedImage(textWidth * 2,
+ textHeight, BufferedImage.TYPE_INT_ARGB);
+ Graphics2D grWide = wideImage.createGraphics();
+ grWide.drawImage(image, 0, 0, wideImage.getWidth(),
+ wideImage.getHeight(), null);
+ grWide.dispose();
+ leftImage = wideImage.getSubimage(0, 0, textWidth, textHeight);
+ rightImage = wideImage.getSubimage(textWidth, 0, textWidth,
+ textHeight);
+ break;
+ }
+ left.setImage(leftImage);
+ right.setImage(rightImage);
+ // Since we have image data, ditch the character here. Otherwise, a
+ // drawBoxShadow() over the terminal window will show the characters
+ // which looks wrong.
+ left.setChar(' ');
+ right.setChar(' ');
+ putCharXY(x, y, left);
+ putCharXY(x + 1, y, right);
+ }
+
+ /**
+ * Set up the double-width font.
+ *
+ * @param fontSize the size of font to request for the single-width font.
+ * The double-width font will be 2x this value.
+ */
+ private void setupFont(final int fontSize) {
+ doubleFont = GlyphMaker.getInstance(fontSize * 2);
+
+ // Special case: the ECMA48 backend needs to have a timer to drive
+ // its blink state.
+ if (getScreen() instanceof jexer.backend.ECMA48Terminal) {
+ if (!haveTimer) {
+ // Blink every 500 millis.
+ long millis = 500;
+ getApplication().addTimer(millis, true,
+ new TAction() {
+ public void DO() {
+ blinkState = !blinkState;
+ getApplication().doRepaint();
+ }
+ }
+ );
+ haveTimer = true;
+ }
+ }
+ }
+
+ // ------------------------------------------------------------------------
+ // DisplayListener --------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Called by emulator when fresh data has come in.
+ */
+ public void displayChanged() {
+ dirty = true;
+ getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
+ }
+
+ /**
+ * Function to call to obtain the display width.
+ *
+ * @return the number of columns in the display
+ */
+ public int getDisplayWidth() {
+ if (ptypipe) {
+ return getWidth() - 2;
+ }
+ return 80;
+ }
+
+ /**
+ * Function to call to obtain the display height.
+ *
+ * @return the number of rows in the display
+ */
+ public int getDisplayHeight() {
+ if (ptypipe) {
+ return getHeight() - 2;
+ }
+ return 24;
+ }
+