Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / jexer / TTerminalWindow.java
index 58e60e567c15387ffd82379f696af8e1f303ff5a..754b7a512d6f7581216a78b76c2a8a7be3838dcb 100644 (file)
-/**
+/*
  * Jexer - Java Text User Interface
  *
- * License: LGPLv3 or later
- *
- * This module is licensed under the GNU Lesser General Public License
- * Version 3.  Please see the file "COPYING" in this directory for more
- * information about the GNU Lesser General Public License Version 3.
+ * The MIT License (MIT)
  *
- *     Copyright (C) 2015  Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 3 of
- * the License, or (at your option) any later version.
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
  *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, see
- * http://www.gnu.org/licenses/, or write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  *
  * @author Kevin Lamonte [kevin.lamonte@gmail.com]
  * @version 1
  */
 package jexer;
 
-import java.io.InputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.ResourceBundle;
 
-import jexer.bits.Cell;
-import jexer.bits.CellAttributes;
+import jexer.menu.TMenu;
 import jexer.event.TKeypressEvent;
+import jexer.event.TMenuEvent;
 import jexer.event.TMouseEvent;
 import jexer.event.TResizeEvent;
-import jexer.tterminal.DisplayLine;
-import jexer.tterminal.ECMA48;
+import static jexer.TCommand.*;
 import static jexer.TKeypress.*;
 
 /**
  * TTerminalWindow exposes a ECMA-48 / ANSI X3.64 style terminal in a window.
  */
-public class TTerminalWindow extends TWindow {
+public class TTerminalWindow extends TScrollableWindow {
 
     /**
-     * The emulator.
+     * Translated strings.
      */
-    private ECMA48 emulator;
+    private static final ResourceBundle i18n = ResourceBundle.getBundle(TTerminalWindow.class.getName());
+
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
-     * The Process created by the shell spawning constructor.
+     * The terminal.
      */
-    private Process shell;
+    private TTerminalWidget terminal;
 
     /**
-     * Vertical scrollbar.
+     * If true, close the window when the shell exits.
      */
-    private TVScroller vScroller;
+    private boolean closeOnExit = false;
+
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
-     * Public constructor spawns a shell.
+     * Public constructor spawns a custom command line.
      *
      * @param application TApplication that manages this window
      * @param x column relative to parent
      * @param y row relative to parent
-     * @param flags mask of CENTERED, MODAL, or RESIZABLE
+     * @param commandLine the command line to execute
      */
     public TTerminalWindow(final TApplication application, final int x,
-        final int y, final int flags) {
+        final int y, final String commandLine) {
 
-        super(application, "Terminal", x, y, 80 + 2, 24 + 2, flags);
-
-        try {
-            String [] cmdShellWindows = {
-                "cmd.exe"
-            };
-
-            // You cannot run a login shell in a bare Process interactively,
-            // due to libc's behavior of buffering when stdin/stdout aren't a
-            // tty.  Use 'script' instead to run a shell in a pty.
-            String [] cmdShell = {
-                "script", "-fqe", "/dev/null"
-            };
-            // Spawn a shell and pass its I/O to the other constructor.
-            ProcessBuilder pb;
-            if (System.getProperty("os.name").startsWith("Windows")) {
-                pb = new ProcessBuilder(cmdShellWindows);
-            } else {
-                pb = new ProcessBuilder(cmdShell);
-            }
-            // shell = Runtime.getRuntime().exec(cmdShell);
-
-            // TODO: add LANG, TERM, LINES, and COLUMNS
-            pb.redirectErrorStream(true);
-            shell = pb.start();
-            emulator = new ECMA48(ECMA48.DeviceType.XTERM,
-                shell.getInputStream(),
-                shell.getOutputStream());
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
+        this(application, x, y, RESIZABLE, commandLine.split("\\s+"),
+            System.getProperty("jexer.TTerminal.closeOnExit",
+                "false").equals("true"));
+    }
+
+    /**
+     * Public constructor spawns a custom command line.
+     *
+     * @param application TApplication that manages this window
+     * @param x column relative to parent
+     * @param y row relative to parent
+     * @param commandLine the command line to execute
+     * @param closeOnExit if true, close the window when the command exits
+     */
+    public TTerminalWindow(final TApplication application, final int x,
+        final int y, final String commandLine, final boolean closeOnExit) {
 
-        // Setup the scroll bars
-        onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
-                getHeight()));
+        this(application, x, y, RESIZABLE, commandLine.split("\\s+"),
+            closeOnExit);
     }
 
     /**
-     * Public constructor.
+     * Public constructor spawns a custom command line.
      *
      * @param application TApplication that manages this window
      * @param x column relative to parent
      * @param y row relative to parent
      * @param flags mask of CENTERED, MODAL, or RESIZABLE
-     * @param input an InputStream connected to the remote side.  For type ==
-     * XTERM, input is converted to a Reader with UTF-8 encoding.
-     * @param output an OutputStream connected to the remote user.  For type
-     * == XTERM, output is converted to a Writer with UTF-8 encoding.
-     * @throws UnsupportedEncodingException if an exception is thrown when
-     * creating the InputStreamReader
+     * @param command the command line to execute
      */
     public TTerminalWindow(final TApplication application, final int x,
-        final int y, final int flags, final InputStream input,
-        final OutputStream output) throws UnsupportedEncodingException {
+        final int y, final int flags, final String [] command) {
 
-        super(application, "Terminal", x, y, 80 + 2, 24 + 2, flags);
+        this(application, x, y, flags, command,
+            System.getProperty("jexer.TTerminal.closeOnExit",
+                "false").equals("true"));
+    }
+
+    /**
+     * Public constructor spawns a custom command line.
+     *
+     * @param application TApplication that manages this window
+     * @param x column relative to parent
+     * @param y row relative to parent
+     * @param flags mask of CENTERED, MODAL, or RESIZABLE
+     * @param command the command line to execute
+     * @param closeOnExit if true, close the window when the command exits
+     */
+    public TTerminalWindow(final TApplication application, final int x,
+        final int y, final int flags, final String [] command,
+        final boolean closeOnExit) {
+
+        super(application, i18n.getString("windowTitle"), x, y,
+            80 + 2, 24 + 2, flags);
+
+        // Require at least one line for the display.
+        setMinimumWindowHeight(3);
 
-        emulator = new ECMA48(ECMA48.DeviceType.XTERM, input, output);
+        this.closeOnExit = closeOnExit;
+        vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
 
-        // Setup the scroll bars
-        onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
-                getHeight()));
+        // Claim the keystrokes the emulator will need.
+        addShortcutKeys();
 
+        // Add shortcut text
+        TStatusBar statusBar = newStatusBar(i18n.getString("statusBarRunning"));
+        statusBar.addShortcutKeypress(kbF1, cmHelp,
+            i18n.getString("statusBarHelp"));
+        statusBar.addShortcutKeypress(kbF10, cmMenu,
+            i18n.getString("statusBarMenu"));
+
+        // Spin it up
+        terminal = new TTerminalWidget(this, 0, 0, command, new TAction() {
+            public void DO() {
+                onShellExit();
+            }
+        });
     }
 
     /**
-     * Draw the display buffer.
+     * Public constructor spawns a shell.
+     *
+     * @param application TApplication that manages this window
+     * @param x column relative to parent
+     * @param y row relative to parent
+     * @param flags mask of CENTERED, MODAL, or RESIZABLE
      */
-    @Override
-    public void draw() {
-        // Synchronize against the emulator so we don't stomp on its reader
-        // thread.
-        synchronized (emulator) {
-
-            // Update the scroll bars
-            reflow();
-
-            // Draw the box using my superclass
-            super.draw();
-
-            List<DisplayLine> scrollback = emulator.getScrollbackBuffer();
-            List<DisplayLine> display = emulator.getDisplayBuffer();
-
-            // Put together the visible rows
-            // System.err.printf("----------------------------\n");
-            // System.err.printf("vScroller.value %d\n", vScroller.getValue());
-            int visibleHeight = getHeight() - 2;
-            // System.err.printf("visibleHeight %d\n", visibleHeight);
-            int visibleBottom = scrollback.size() + display.size()
-                + vScroller.getValue();
-            // System.err.printf("visibleBottom %d\n", visibleBottom);
-            assert (visibleBottom >= 0);
-
-            List<DisplayLine> preceedingBlankLines = new LinkedList<DisplayLine>();
-            int visibleTop = visibleBottom - visibleHeight;
-            // System.err.printf("visibleTop %d\n", visibleTop);
-            if (visibleTop < 0) {
-                for (int i = visibleTop; i < 0; i++) {
-                    preceedingBlankLines.add(emulator.getBlankDisplayLine());
-                }
-                visibleTop = 0;
-            }
-            assert (visibleTop >= 0);
-
-            List<DisplayLine> displayLines = new LinkedList<DisplayLine>();
-            displayLines.addAll(scrollback);
-            displayLines.addAll(display);
-            // System.err.printf("displayLines.size %d\n", displayLines.size());
-
-            List<DisplayLine> visibleLines = new LinkedList<DisplayLine>();
-            visibleLines.addAll(preceedingBlankLines);
-            visibleLines.addAll(displayLines.subList(visibleTop,
-                    visibleBottom));
-            // System.err.printf("visibleLines.size %d\n", visibleLines.size());
-
-            visibleHeight -= visibleLines.size();
-            // System.err.printf("visibleHeight %d\n", visibleHeight);
-            assert (visibleHeight >= 0);
-
-            // Now draw the emulator screen
-            int row = 1;
-            for (DisplayLine line: visibleLines) {
-                int widthMax = emulator.getWidth();
-                if (line.isDoubleWidth()) {
-                    widthMax /= 2;
-                }
-                if (widthMax > getWidth() - 2) {
-                    widthMax = getWidth() - 2;
-                }
-                for (int i = 0; i < widthMax; i++) {
-                    Cell ch = line.charAt(i);
-                    Cell newCell = new Cell();
-                    newCell.setTo(ch);
-                    boolean reverse = line.isReverseColor() ^ ch.getReverse();
-                    newCell.setReverse(false);
-                    if (reverse) {
-                        newCell.setBackColor(ch.getForeColor());
-                        newCell.setForeColor(ch.getBackColor());
-                    }
-                    if (line.isDoubleWidth()) {
-                        getScreen().putCharXY((i * 2) + 1, row, newCell);
-                        getScreen().putCharXY((i * 2) + 2, row, ' ', newCell);
-                    } else {
-                        getScreen().putCharXY(i + 1, row, newCell);
-                    }
-                }
-                row++;
-                if (row == getHeight() - 1) {
-                    // Don't overwrite the box edge
-                    break;
-                }
-            }
-            CellAttributes background = new CellAttributes();
-            // Fill in the blank lines on bottom
-            for (int i = 0; i < visibleHeight; i++) {
-                getScreen().hLineXY(1, i + row, getWidth() - 2, ' ',
-                    background);
-            }
+    public TTerminalWindow(final TApplication application, final int x,
+        final int y, final int flags) {
 
-        } // synchronized (emulator)
+        this(application, x, y, flags,
+            System.getProperty("jexer.TTerminal.closeOnExit",
+                "false").equals("true"));
 
     }
 
     /**
-     * Handle window close.
+     * Public constructor spawns a shell.
+     *
+     * @param application TApplication that manages this window
+     * @param x column relative to parent
+     * @param y row relative to parent
+     * @param flags mask of CENTERED, MODAL, or RESIZABLE
+     * @param closeOnExit if true, close the window when the shell exits
      */
-    @Override public void onClose() {
-        emulator.close();
+    public TTerminalWindow(final TApplication application, final int x,
+        final int y, final int flags, final boolean closeOnExit) {
+
+        super(application, i18n.getString("windowTitle"), x, y,
+            80 + 2, 24 + 2, flags);
+
+        // Require at least one line for the display.
+        setMinimumWindowHeight(3);
+
+        this.closeOnExit = closeOnExit;
+        vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
+
+        // Claim the keystrokes the emulator will need.
+        addShortcutKeys();
+
+        // Add shortcut text
+        TStatusBar statusBar = newStatusBar(i18n.getString("statusBarRunning"));
+        statusBar.addShortcutKeypress(kbF1, cmHelp,
+            i18n.getString("statusBarHelp"));
+        statusBar.addShortcutKeypress(kbF10, cmMenu,
+            i18n.getString("statusBarMenu"));
+
+        // Spin it up
+        terminal = new TTerminalWidget(this, 0, 0, new TAction() {
+            public void DO() {
+                onShellExit();
+            }
+        });
     }
 
+    // ------------------------------------------------------------------------
+    // TScrollableWindow ------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
-     * Copy out variables from the emulator that TTerminal has to expose on
-     * screen.
+     * Draw the display buffer.
      */
-    private void readEmulatorState() {
-        // Synchronize against the emulator so we don't stomp on its reader
-        // thread.
-        synchronized (emulator) {
-
-            setCursorX(emulator.getCursorX() + 1);
-            setCursorY(emulator.getCursorY() + 1
-                + (getHeight() - 2 - emulator.getHeight()));
-            if (vScroller != null) {
-                setCursorY(getCursorY() - vScroller.getValue());
-            }
-            setHasCursor(emulator.visibleCursor());
-            if (getCursorX() > getWidth() - 2) {
-                setHasCursor(false);
-            }
-            if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
-                setHasCursor(false);
-            }
-            if (emulator.getScreenTitle().length() > 0) {
-                // Only update the title if the shell is still alive
-                if (shell != null) {
-                    setTitle(emulator.getScreenTitle());
-                }
-            }
-            setMaximumWindowWidth(emulator.getWidth() + 2);
-
-            // Check to see if the shell has died.
-            if (!emulator.isReading() && (shell != null)) {
-                // The emulator exited on its own, all is fine
-                setTitle(String.format("%s [Completed - %d]",
-                        getTitle(), shell.exitValue()));
-                shell = null;
-                emulator.close();
-            } 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(String.format("%s [Completed - %d]",
-                            getTitle(), rc));
-                    shell = null;
-                    emulator.close();
-                } catch (IllegalThreadStateException e) {
-                    // The shell is still running, do nothing.
-                }
-            }
-
-        } // synchronized (emulator)
+    @Override
+    public void draw() {
+        if (terminal != null) {
+            setTitle(terminal.getTitle());
+        }
+        reflowData();
+        super.draw();
     }
 
     /**
@@ -308,51 +235,32 @@ public class TTerminalWindow extends TWindow {
      */
     @Override
     public void onResize(final TResizeEvent resize) {
-
-        // Synchronize against the emulator so we don't stomp on its reader
-        // thread.
-        synchronized (emulator) {
-
-            if (resize.getType() == TResizeEvent.Type.WIDGET) {
-                // Resize the scroll bars
-                reflow();
-
-                // Get out of scrollback
-                vScroller.setValue(0);
+        if (resize.getType() == TResizeEvent.Type.WIDGET) {
+            if (terminal != null) {
+                terminal.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
+                        getWidth() - 2, getHeight() - 2));
             }
-            return;
 
-        } // synchronized (emulator)
+            // Resize the scroll bars
+            reflowData();
+            placeScrollbars();
+        }
+        return;
     }
 
     /**
      * Resize scrollbars for a new width/height.
      */
-    private void reflow() {
-
-        // Synchronize against the emulator so we don't stomp on its reader
-        // thread.
-        synchronized (emulator) {
-
-            // Pull cursor information
-            readEmulatorState();
-
-            // Vertical scrollbar
-            if (vScroller == null) {
-                vScroller = new TVScroller(this, getWidth() - 2, 0,
-                    getHeight() - 2);
-                vScroller.setBottomValue(0);
-                vScroller.setValue(0);
-            } else {
-                vScroller.setX(getWidth() - 2);
-                vScroller.setHeight(getHeight() - 2);
-            }
-            vScroller.setTopValue(getHeight() - 2
-                - (emulator.getScrollbackBuffer().size()
-                    + emulator.getDisplayBuffer().size()));
-            vScroller.setBigChange(getHeight() - 2);
-
-        } // synchronized (emulator)
+    @Override
+    public void reflowData() {
+        // Vertical scrollbar
+        if (terminal != null) {
+            terminal.reflowData();
+            setTopValue(terminal.getTopValue());
+            setBottomValue(terminal.getBottomValue());
+            setVerticalBigChange(terminal.getVerticalBigChange());
+            setVerticalValue(terminal.getVerticalValue());
+        }
     }
 
     /**
@@ -362,67 +270,211 @@ public class TTerminalWindow extends TWindow {
      */
     @Override
     public void onKeypress(final TKeypressEvent keypress) {
-
-        // Scrollback up/down
-        if (keypress.equals(kbShiftPgUp)
-            || keypress.equals(kbCtrlPgUp)
-            || keypress.equals(kbAltPgUp)
+        if ((terminal != null)
+            && (terminal.isReading())
+            && (!inKeyboardResize)
         ) {
-            vScroller.bigDecrement();
+            terminal.onKeypress(keypress);
+        } else {
+            super.onKeypress(keypress);
+        }
+    }
+
+    /**
+     * Handle mouse press events.
+     *
+     * @param mouse mouse button press event
+     */
+    @Override
+    public void onMouseDown(final TMouseEvent mouse) {
+        if (inWindowMove || inWindowResize) {
+            // TWindow needs to deal with this.
+            super.onMouseDown(mouse);
             return;
         }
-        if (keypress.equals(kbShiftPgDn)
-            || keypress.equals(kbCtrlPgDn)
-            || keypress.equals(kbAltPgDn)
-        ) {
-            vScroller.bigIncrement();
+
+        super.onMouseDown(mouse);
+    }
+
+    /**
+     * Handle mouse release events.
+     *
+     * @param mouse mouse button release event
+     */
+    @Override
+    public void onMouseUp(final TMouseEvent mouse) {
+        if (inWindowMove || inWindowResize) {
+            // TWindow needs to deal with this.
+            super.onMouseUp(mouse);
             return;
         }
 
-        // Synchronize against the emulator so we don't stomp on its reader
-        // thread.
-        synchronized (emulator) {
-            if (emulator.isReading()) {
-                // Get out of scrollback
-                vScroller.setValue(0);
-                emulator.keypress(keypress.getKey());
-
-                // UGLY HACK TIME!  cmd.exe needs CRLF, not just CR, so if
-                // this is kBEnter then also send kbCtrlJ.
-                if (System.getProperty("os.name").startsWith("Windows")) {
-                    if (keypress.equals(kbEnter)) {
-                        emulator.keypress(kbCtrlJ);
-                    }
-                }
-
-                readEmulatorState();
-                return;
+        super.onMouseUp(mouse);
+
+        if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
+            // Clicked on vertical scrollbar
+            if (terminal != null) {
+                terminal.setVerticalValue(getVerticalValue());
             }
         }
+    }
 
-        // Process is closed, honor "normal" TUI keystrokes
-        super.onKeypress(keypress);
+    /**
+     * Handle mouse motion events.
+     *
+     * @param mouse mouse motion event
+     */
+    @Override
+    public void onMouseMotion(final TMouseEvent mouse) {
+        if (inWindowMove || inWindowResize) {
+            // TWindow needs to deal with this.
+            super.onMouseMotion(mouse);
+            return;
+        }
+
+        super.onMouseMotion(mouse);
+
+        if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
+            // Clicked/dragged on vertical scrollbar
+            if (terminal != null) {
+                terminal.setVerticalValue(getVerticalValue());
+            }
+        }
     }
 
     /**
-     * Handle mouse press events.
+     * Get this window's help topic to load.
      *
-     * @param mouse mouse button press event
+     * @return the topic name
      */
     @Override
-    public void onMouseDown(final TMouseEvent mouse) {
+    public String getHelpTopic() {
+        return "Terminal Window";
+    }
 
-        if (mouse.getMouseWheelUp()) {
-            vScroller.decrement();
-            return;
+    // ------------------------------------------------------------------------
+    // TTerminalWindow --------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Returns true if this window does not want the application-wide mouse
+     * cursor drawn over it.
+     *
+     * @return true if this window does not want the application-wide mouse
+     * cursor drawn over it
+     */
+    @Override
+    public boolean hasHiddenMouse() {
+        if (terminal != null) {
+            return terminal.hasHiddenMouse();
         }
-        if (mouse.getMouseWheelDown()) {
-            vScroller.increment();
-            return;
+        return false;
+    }
+
+    /**
+     * 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);
+    }
+
+    /**
+     * Hook for subclasses to be notified of the shell termination.
+     */
+    public void onShellExit() {
+        if (closeOnExit) {
+            close();
         }
+        clearShortcutKeypresses();
+        getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
+    }
 
-        // Pass to children
-        super.onMouseDown(mouse);
+    /**
+     * Wait for a period of time to get output from the launched process.
+     *
+     * @param millis millis to wait for, or 0 to wait forever
+     * @return true if the launched process has emitted something
+     */
+    public boolean waitForOutput(final int millis) {
+        if (terminal == null) {
+            return false;
+        }
+        return terminal.waitForOutput(millis);
+    }
+
+    /**
+     * Get the exit value for the emulator.
+     *
+     * @return exit value
+     */
+    public int getExitValue() {
+        if (terminal == null) {
+            return -1;
+        }
+        return terminal.getExitValue();
     }
 
 }