first screenshot attempt
[fanfix.git] / src / jexer / TApplication.java
index 88b329f6f62158668562b821be59d235b67725c5..5813d6f3ca15402ebbee2fbc0f592744c4c5e8e3 100644 (file)
@@ -64,6 +64,16 @@ import static jexer.TKeypress.*;
  */
 public class TApplication {
 
+    /**
+     * If true, emit thread stuff to System.err.
+     */
+    private static final boolean debugThreads = false;
+
+    /**
+     * If true, emit events being processed to System.err.
+     */
+    private static final boolean debugEvents = false;
+
     /**
      * WidgetEventHandler is the main event consumer loop.  There are at most
      * two such threads in existence: the primary for normal case and a
@@ -110,12 +120,33 @@ public class TApplication {
                                 break;
                             }
                         }
-                        synchronized (application) {
-                            application.wait();
+
+                        synchronized (this) {
+                            /*
+                            System.err.printf("%s %s sleep\n", this, primary ?
+                                "primary" : "secondary");
+                             */
+
+                            this.wait();
+
+                            /*
+                            System.err.printf("%s %s AWAKE\n", this, primary ?
+                                "primary" : "secondary");
+                             */
+
                             if ((!primary)
                                 && (application.secondaryEventReceiver == null)
                             ) {
-                                // Secondary thread, time to exit
+                                // Secondary thread, emergency exit.  If we
+                                // got here then something went wrong with
+                                // the handoff between yield() and
+                                // closeWindow().
+
+                                System.err.printf("secondary exiting at wrong time, why?\n");
+                                synchronized (application.primaryEventHandler) {
+                                    application.primaryEventHandler.notify();
+                                }
+                                application.secondaryEventHandler = null;
                                 return;
                             }
                             break;
@@ -134,18 +165,47 @@ public class TApplication {
                         }
                         event = application.drainEventQueue.remove(0);
                     }
+                    // Wait for drawAll() or doIdle() to be done, then handle
+                    // the event.
+                    boolean oldLock = lockHandleEvent();
+                    assert (oldLock == false);
                     if (primary) {
                         primaryHandleEvent(event);
                     } else {
                         secondaryHandleEvent(event);
                     }
+                    application.repaint = true;
                     if ((!primary)
                         && (application.secondaryEventReceiver == null)
                     ) {
-                        // Secondary thread, time to exit
+                        // Secondary thread, time to exit.
+
+                        // DO NOT UNLOCK.  Primary thread just came back from
+                        // primaryHandleEvent() and will unlock in the else
+                        // block below.  Just wake it up.
+                        synchronized (application.primaryEventHandler) {
+                            application.primaryEventHandler.notify();
+                        }
+                        // Now eliminate my reference so that
+                        // wakeEventHandler() resumes working on the primary.
+                        application.secondaryEventHandler = null;
+
+                        // All done!
                         return;
+                    } else {
+                        // Unlock.  Either I am primary thread, or I am
+                        // secondary thread and still running.
+                        oldLock = unlockHandleEvent();
+                        assert (oldLock == true);
                     }
+                } // for (;;)
+
+                // I have done some work of some kind.  Tell the main run()
+                // loop to wake up now.
+                synchronized (application) {
+                    application.notify();
                 }
+
             } // while (true) (main runnable loop)
         }
     }
@@ -153,17 +213,123 @@ public class TApplication {
     /**
      * The primary event handler thread.
      */
-    private WidgetEventHandler primaryEventHandler;
+    private volatile WidgetEventHandler primaryEventHandler;
 
     /**
      * The secondary event handler thread.
      */
-    private WidgetEventHandler secondaryEventHandler;
+    private volatile WidgetEventHandler secondaryEventHandler;
 
     /**
      * The widget receiving events from the secondary event handler thread.
      */
-    private TWidget secondaryEventReceiver;
+    private volatile TWidget secondaryEventReceiver;
+
+    /**
+     * Spinlock for the primary and secondary event handlers.
+     * WidgetEventHandler.run() is responsible for setting this value.
+     */
+    private volatile boolean insideHandleEvent = false;
+
+    /**
+     * Wake the sleeping active event handler.
+     */
+    private void wakeEventHandler() {
+        if (secondaryEventHandler != null) {
+            synchronized (secondaryEventHandler) {
+                secondaryEventHandler.notify();
+            }
+        } else {
+            assert (primaryEventHandler != null);
+            synchronized (primaryEventHandler) {
+                primaryEventHandler.notify();
+            }
+        }
+    }
+
+    /**
+     * Set the insideHandleEvent flag to true.  lockoutEventHandlers() will
+     * spin indefinitely until unlockHandleEvent() is called.
+     *
+     * @return the old value of insideHandleEvent
+     */
+    private boolean lockHandleEvent() {
+        if (debugThreads) {
+            System.err.printf("  >> lockHandleEvent(): oldValue %s",
+                insideHandleEvent);
+        }
+        boolean oldValue = true;
+
+        synchronized (this) {
+            // Wait for TApplication.run() to finish using the global state
+            // before allowing further event processing.
+            while (lockoutHandleEvent == true);
+
+            oldValue = insideHandleEvent;
+            insideHandleEvent = true;
+        }
+
+        if (debugThreads) {
+            System.err.printf(" ***\n");
+        }
+        return oldValue;
+    }
+
+    /**
+     * Set the insideHandleEvent flag to false.  lockoutEventHandlers() will
+     * spin indefinitely until unlockHandleEvent() is called.
+     *
+     * @return the old value of insideHandleEvent
+     */
+    private boolean unlockHandleEvent() {
+        if (debugThreads) {
+            System.err.printf("  << unlockHandleEvent(): oldValue %s\n",
+                insideHandleEvent);
+        }
+        synchronized (this) {
+            boolean oldValue = insideHandleEvent;
+            insideHandleEvent = false;
+            return oldValue;
+        }
+    }
+
+    /**
+     * Spinlock for the primary and secondary event handlers.  When true, the
+     * event handlers will spinlock wait before calling handleEvent().
+     */
+    private volatile boolean lockoutHandleEvent = false;
+
+    /**
+     * TApplication.run() needs to be able rely on the global data structures
+     * being intact when calling doIdle() and drawAll().  Tell the event
+     * handlers to wait for an unlock before handling their events.
+     */
+    private void stopEventHandlers() {
+        if (debugThreads) {
+            System.err.printf(">> stopEventHandlers()");
+        }
+
+        lockoutHandleEvent = true;
+        // Wait for the last event to finish processing before returning
+        // control to TApplication.run().
+        while (insideHandleEvent == true);
+
+        if (debugThreads) {
+            System.err.printf(" XXX\n");
+        }
+    }
+
+    /**
+     * TApplication.run() needs to be able rely on the global data structures
+     * being intact when calling doIdle() and drawAll().  Tell the event
+     * handlers that it is now OK to handle their events.
+     */
+    private void startEventHandlers() {
+        if (debugThreads) {
+            System.err.printf("<< startEventHandlers()\n");
+        }
+        lockoutHandleEvent = false;
+    }
 
     /**
      * Access to the physical screen, keyboard, and mouse.
@@ -247,22 +413,16 @@ public class TApplication {
     /**
      * When true, exit the application.
      */
-    private boolean quit = false;
+    private volatile boolean quit = false;
 
     /**
      * When true, repaint the entire screen.
      */
-    private boolean repaint = true;
+    private volatile boolean repaint = true;
 
     /**
-     * Request full repaint on next screen refresh.
-     */
-    public final void setRepaint() {
-        repaint = true;
-    }
-
-    /**
-     * When true, just flush updates from the screen.
+     * When true, just flush updates from the screen.  This is only used to
+     * minimize physical writes for the mouse cursor.
      */
     private boolean flush = false;
 
@@ -312,12 +472,26 @@ public class TApplication {
     public TApplication(final InputStream input,
         final OutputStream output) throws UnsupportedEncodingException {
 
-        if (System.getProperty("jexer.AWT", "false").equals("true")) {
-            backend     = new AWTBackend();
-        } else {
-            backend     = new ECMA48Backend(input, output);
+        // AWT is the default backend on Windows unless explicitly overridden
+        // by jexer.AWT.
+        boolean useAWT = false;
+        if (System.getProperty("os.name").startsWith("Windows")) {
+            useAWT = true;
+        }
+        if (System.getProperty("jexer.AWT") != null) {
+            if (System.getProperty("jexer.AWT", "false").equals("true")) {
+                useAWT = true;
+            } else {
+                useAWT = false;
+            }
         }
 
+
+        if (useAWT) {
+            backend     = new AWTBackend(this);
+        } else {
+            backend     = new ECMA48Backend(this, input, output);
+        }
         theme           = new ColorTheme();
         desktopBottom   = getScreen().getHeight() - 1;
         fillEventQueue  = new ArrayList<TInputEvent>();
@@ -352,14 +526,18 @@ public class TApplication {
      * Draw everything.
      */
     public final void drawAll() {
+        if (debugThreads) {
+            System.err.printf("drawAll() enter\n");
+        }
+
         if ((flush) && (!repaint)) {
             backend.flushScreen();
             flush = false;
             return;
         }
 
-        if (!repaint) {
-            return;
+        if (debugThreads) {
+            System.err.printf("drawAll() REDRAW\n");
         }
 
         // If true, the cursor is not visible
@@ -452,24 +630,48 @@ public class TApplication {
         while (!quit) {
             // Timeout is in milliseconds, so default timeout after 1 second
             // of inactivity.
-            int timeout = getSleepTime(1000);
-
-            // See if there are any definitely events waiting to be processed
-            // or a screen redraw to do.  If so, do not wait if there is no
-            // I/O coming in.
-            synchronized (drainEventQueue) {
-                if (drainEventQueue.size() > 0) {
-                    timeout = 0;
+            long timeout = 0;
+
+            // If I've got no updates to render, wait for something from the
+            // backend or a timer.
+            if (!repaint && !flush) {
+                // Never sleep longer than 100 millis, to get windows with
+                // background tasks an opportunity to update the display.
+                timeout = getSleepTime(100);
+
+                // See if there are any definitely events waiting to be
+                // processed.  If so, do not wait -- either there is I/O
+                // coming in or the primary/secondary threads are still
+                // working.
+                synchronized (drainEventQueue) {
+                    if (drainEventQueue.size() > 0) {
+                        timeout = 0;
+                    }
+                }
+                synchronized (fillEventQueue) {
+                    if (fillEventQueue.size() > 0) {
+                        timeout = 0;
+                    }
                 }
             }
-            synchronized (fillEventQueue) {
-                if (fillEventQueue.size() > 0) {
-                    timeout = 0;
+
+            if (timeout > 0) {
+                // As of now, I've got nothing to do: no I/O, nothing from
+                // the consumer threads, no timers that need to run ASAP.  So
+                // wait until either the backend or the consumer threads have
+                // something to do.
+                try {
+                    synchronized (this) {
+                        this.wait(timeout);
+                    }
+                } catch (InterruptedException e) {
+                    // I'm awake and don't care why, let's see what's going
+                    // on out there.
                 }
             }
 
             // Pull any pending I/O events
-            backend.getEvents(fillEventQueue, timeout);
+            backend.getEvents(fillEventQueue);
 
             // Dispatch each event to the appropriate handler, one at a time.
             for (;;) {
@@ -483,19 +685,48 @@ public class TApplication {
                 metaHandleEvent(event);
             }
 
+            // Wake a consumer thread if we have any pending events.
+            synchronized (drainEventQueue) {
+                if (drainEventQueue.size() > 0) {
+                    wakeEventHandler();
+                }
+            }
+
+            // Prevent stepping on the primary or secondary event handler.
+            stopEventHandlers();
+
             // Process timers and call doIdle()'s
             doIdle();
 
             // Update the screen
-            drawAll();
-        }
+            synchronized (getScreen()) {
+                drawAll();
+            }
 
-        // Shutdown the consumer threads
-        synchronized (this) {
-            this.notifyAll();
+            // Let the event handlers run again.
+            startEventHandlers();
+
+        } // while (!quit)
+
+        // Shutdown the event consumer threads
+        if (secondaryEventHandler != null) {
+            synchronized (secondaryEventHandler) {
+                secondaryEventHandler.notify();
+            }
+        }
+        if (primaryEventHandler != null) {
+            synchronized (primaryEventHandler) {
+                primaryEventHandler.notify();
+            }
         }
 
+        // Shutdown the user I/O thread(s)
         backend.shutdown();
+
+        // Close all the windows.  This gives them an opportunity to release
+        // resources.
+        closeAllWindows();
+
     }
 
     /**
@@ -506,10 +737,10 @@ public class TApplication {
      */
     private void metaHandleEvent(final TInputEvent event) {
 
-        /*
-        System.err.printf(String.format("metaHandleEvents event: %s\n",
-                event)); System.err.flush();
-         */
+        if (debugEvents) {
+            System.err.printf(String.format("metaHandleEvents event: %s\n",
+                    event)); System.err.flush();
+        }
 
         if (quit) {
             // Do no more processing if the application is already trying
@@ -554,13 +785,6 @@ public class TApplication {
         synchronized (drainEventQueue) {
             drainEventQueue.add(event);
         }
-
-        // Wake all threads: primary thread will either be consuming events
-        // again or waiting in yield(), and secondary thread will either not
-        // exist or consuming events.
-        synchronized (this) {
-            this.notifyAll();
-        }
     }
 
     /**
@@ -573,7 +797,9 @@ public class TApplication {
      */
     private void primaryHandleEvent(final TInputEvent event) {
 
-        // System.err.printf("Handle event: %s\n", event);
+        if (debugEvents) {
+            System.err.printf("Handle event: %s\n", event);
+        }
 
         // Special application-wide events -----------------------------------
 
@@ -666,7 +892,10 @@ public class TApplication {
                     mouse.setX(mouse.getX() - window.getX());
                     mouse.setY(mouse.getY() - window.getY());
                 }
-                // System.err("TApplication dispatch event: %s\n", event);
+                if (debugEvents) {
+                    System.err.printf("TApplication dispatch event: %s\n",
+                        event);
+                }
                 window.handleEvent(event);
                 break;
             }
@@ -706,26 +935,41 @@ public class TApplication {
      */
     public final void yield() {
         assert (secondaryEventReceiver != null);
+        // This is where we handoff the event handler lock from the primary
+        // to secondary thread.  We unlock here, and in a future loop the
+        // secondary thread locks again.  When it gives up, we have the
+        // single lock back.
+        boolean oldLock = unlockHandleEvent();
+        assert (oldLock == true);
+
+        // System.err.printf("YIELD\n");
+
         while (secondaryEventReceiver != null) {
-            synchronized (this) {
+            synchronized (primaryEventHandler) {
                 try {
-                    this.wait();
+                    primaryEventHandler.wait();
                 } catch (InterruptedException e) {
                     // SQUASH
                 }
             }
         }
+
+        // System.err.printf("EXIT YIELD\n");
     }
 
     /**
      * Do stuff when there is no user input.
      */
     private void doIdle() {
+        if (debugThreads) {
+            System.err.printf("doIdle()\n");
+        }
+
         // Now run any timers that have timed out
         Date now = new Date();
         List<TTimer> keepTimers = new LinkedList<TTimer>();
         for (TTimer timer: timers) {
-            if (timer.getNextTick().getTime() < now.getTime()) {
+            if (timer.getNextTick().getTime() <= now.getTime()) {
                 timer.tick();
                 if (timer.recurring) {
                     keepTimers.add(timer);
@@ -745,24 +989,27 @@ public class TApplication {
     /**
      * Get the amount of time I can sleep before missing a Timer tick.
      *
-     * @param timeout = initial (maximum) timeout
+     * @param timeout = initial (maximum) timeout in millis
      * @return number of milliseconds between now and the next timer event
      */
-    protected int getSleepTime(final int timeout) {
+    protected long getSleepTime(final long timeout) {
         Date now = new Date();
+        long nowTime = now.getTime();
         long sleepTime = timeout;
         for (TTimer timer: timers) {
-            if (timer.getNextTick().getTime() < now.getTime()) {
+            long nextTickTime = timer.getNextTick().getTime();
+            if (nextTickTime < nowTime) {
                 return 0;
             }
-            if ((timer.getNextTick().getTime() > now.getTime())
-                && ((timer.getNextTick().getTime() - now.getTime()) < sleepTime)
-            ) {
-                sleepTime = timer.getNextTick().getTime() - now.getTime();
+
+            long timeDifference = nextTickTime - nowTime;
+            if (timeDifference < sleepTime) {
+                sleepTime = timeDifference;
             }
         }
         assert (sleepTime >= 0);
-        return (int)sleepTime;
+        assert (sleepTime <= timeout);
+        return sleepTime;
     }
 
     /**
@@ -772,20 +1019,22 @@ public class TApplication {
      * @param window the window to remove
      */
     public final void closeWindow(final TWindow window) {
-        int z = window.getZ();
-        window.setZ(-1);
-        Collections.sort(windows);
-        windows.remove(0);
-        TWindow activeWindow = null;
-        for (TWindow w: windows) {
-            if (w.getZ() > z) {
-                w.setZ(w.getZ() - 1);
-                if (w.getZ() == 0) {
-                    w.setActive(true);
-                    assert (activeWindow == null);
-                    activeWindow = w;
-                } else {
-                    w.setActive(false);
+        synchronized (windows) {
+            int z = window.getZ();
+            window.setZ(-1);
+            Collections.sort(windows);
+            windows.remove(0);
+            TWindow activeWindow = null;
+            for (TWindow w: windows) {
+                if (w.getZ() > z) {
+                    w.setZ(w.getZ() - 1);
+                    if (w.getZ() == 0) {
+                        w.setActive(true);
+                        assert (activeWindow == null);
+                        activeWindow = w;
+                    } else {
+                        w.setActive(false);
+                    }
                 }
             }
         }
@@ -804,10 +1053,10 @@ public class TApplication {
             // window is closed.
             secondaryEventReceiver = null;
 
-            // Wake all threads: primary thread will be consuming events
-            // again, and secondary thread will exit.
-            synchronized (this) {
-                this.notifyAll();
+            // Wake the secondary thread, it will wake the primary as it
+            // exits.
+            synchronized (secondaryEventHandler) {
+                secondaryEventHandler.notify();
             }
         }
     }
@@ -824,35 +1073,39 @@ public class TApplication {
             return;
         }
 
-        // Swap z/active between active window and the next in the list
-        int activeWindowI = -1;
-        for (int i = 0; i < windows.size(); i++) {
-            if (windows.get(i).getActive()) {
-                activeWindowI = i;
-                break;
+        synchronized (windows) {
+
+            // Swap z/active between active window and the next in the list
+            int activeWindowI = -1;
+            for (int i = 0; i < windows.size(); i++) {
+                if (windows.get(i).getActive()) {
+                    activeWindowI = i;
+                    break;
+                }
             }
-        }
-        assert (activeWindowI >= 0);
+            assert (activeWindowI >= 0);
 
-        // Do not switch if a window is modal
-        if (windows.get(activeWindowI).isModal()) {
-            return;
-        }
+            // Do not switch if a window is modal
+            if (windows.get(activeWindowI).isModal()) {
+                return;
+            }
 
-        int nextWindowI;
-        if (forward) {
-            nextWindowI = (activeWindowI + 1) % windows.size();
-        } else {
-            if (activeWindowI == 0) {
-                nextWindowI = windows.size() - 1;
+            int nextWindowI;
+            if (forward) {
+                nextWindowI = (activeWindowI + 1) % windows.size();
             } else {
-                nextWindowI = activeWindowI - 1;
+                if (activeWindowI == 0) {
+                    nextWindowI = windows.size() - 1;
+                } else {
+                    nextWindowI = activeWindowI - 1;
+                }
             }
-        }
-        windows.get(activeWindowI).setActive(false);
-        windows.get(activeWindowI).setZ(windows.get(nextWindowI).getZ());
-        windows.get(nextWindowI).setZ(0);
-        windows.get(nextWindowI).setActive(true);
+            windows.get(activeWindowI).setActive(false);
+            windows.get(activeWindowI).setZ(windows.get(nextWindowI).getZ());
+            windows.get(nextWindowI).setZ(0);
+            windows.get(nextWindowI).setActive(true);
+
+        } // synchronized (windows)
 
         // Refresh
         repaint = true;
@@ -864,17 +1117,19 @@ public class TApplication {
      * @param window new window to add
      */
     public final void addWindow(final TWindow window) {
-        // Do not allow a modal window to spawn a non-modal window
-        if ((windows.size() > 0) && (windows.get(0).isModal())) {
-            assert (window.isModal());
-        }
-        for (TWindow w: windows) {
-            w.setActive(false);
-            w.setZ(w.getZ() + 1);
+        synchronized (windows) {
+            // Do not allow a modal window to spawn a non-modal window
+            if ((windows.size() > 0) && (windows.get(0).isModal())) {
+                assert (window.isModal());
+            }
+            for (TWindow w: windows) {
+                w.setActive(false);
+                w.setZ(w.getZ() + 1);
+            }
+            windows.add(window);
+            window.setActive(true);
+            window.setZ(0);
         }
-        windows.add(window);
-        window.setActive(true);
-        window.setZ(0);
     }
 
     /**
@@ -1001,29 +1256,31 @@ public class TApplication {
             return;
         }
 
-        Collections.sort(windows);
-        if (windows.get(0).isModal()) {
-            // Modal windows don't switch
-            return;
-        }
+        synchronized (windows) {
+            Collections.sort(windows);
+            if (windows.get(0).isModal()) {
+                // Modal windows don't switch
+                return;
+            }
 
-        for (TWindow window: windows) {
-            assert (!window.isModal());
-            if (window.mouseWouldHit(mouse)) {
-                if (window == windows.get(0)) {
-                    // Clicked on the same window, nothing to do
+            for (TWindow window: windows) {
+                assert (!window.isModal());
+                if (window.mouseWouldHit(mouse)) {
+                    if (window == windows.get(0)) {
+                        // Clicked on the same window, nothing to do
+                        return;
+                    }
+
+                    // We will be switching to another window
+                    assert (windows.get(0).getActive());
+                    assert (!window.getActive());
+                    windows.get(0).setActive(false);
+                    windows.get(0).setZ(window.getZ());
+                    window.setZ(0);
+                    window.setActive(true);
+                    repaint = true;
                     return;
                 }
-
-                // We will be switching to another window
-                assert (windows.get(0).getActive());
-                assert (!window.getActive());
-                windows.get(0).setActive(false);
-                windows.get(0).setZ(window.getZ());
-                window.setZ(0);
-                window.setActive(true);
-                repaint = true;
-                return;
             }
         }
 
@@ -1110,14 +1367,11 @@ public class TApplication {
             return true;
         }
 
-        /*
-         TODO
         if (command.equals(cmShell)) {
-            openTerminal(0, 0, TWindow.Flag.RESIZABLE);
+            openTerminal(0, 0, TWindow.RESIZABLE);
             repaint = true;
             return true;
         }
-         */
 
         if (command.equals(cmTile)) {
             tileWindows();
@@ -1158,14 +1412,11 @@ public class TApplication {
             return true;
         }
 
-        /*
-         TODO
-        if (menu.id == TMenu.MID_SHELL) {
-            openTerminal(0, 0, TWindow.Flag.RESIZABLE);
+        if (menu.getId() == TMenu.MID_SHELL) {
+            openTerminal(0, 0, TWindow.RESIZABLE);
             repaint = true;
             return true;
         }
-         */
 
         if (menu.getId() == TMenu.MID_TILE) {
             tileWindows();
@@ -1273,7 +1524,7 @@ public class TApplication {
      * @param title menu title
      * @return the new menu
      */
-    public final TMenu addMenu(String title) {
+    public final TMenu addMenu(final String title) {
         int x = 0;
         int y = 0;
         TMenu menu = new TMenu(this, x, y, title);
@@ -1337,8 +1588,11 @@ public class TApplication {
         if (activeMenu != null) {
             return;
         }
-        for (TWindow window: windows) {
-            closeWindow(window);
+
+        synchronized (windows) {
+            for (TWindow window: windows) {
+                closeWindow(window);
+            }
         }
     }
 
@@ -1347,54 +1601,54 @@ public class TApplication {
      * almost the same results as Turbo Pascal 7.0's IDE.
      */
     private void tileWindows() {
-        // Don't do anything if we are in the menu
-        if (activeMenu != null) {
-            return;
-        }
-        int z = windows.size();
-        if (z == 0) {
-            return;
-        }
-        int a = 0;
-        int b = 0;
-        a = (int)(Math.sqrt(z));
-        int c = 0;
-        while (c < a) {
-            b = (z - c) / a;
-            if (((a * b) + c) == z) {
-                break;
+        synchronized (windows) {
+            // Don't do anything if we are in the menu
+            if (activeMenu != null) {
+                return;
             }
-            c++;
-        }
-        assert (a > 0);
-        assert (b > 0);
-        assert (c < a);
-        int newWidth = (getScreen().getWidth() / a);
-        int newHeight1 = ((getScreen().getHeight() - 1) / b);
-        int newHeight2 = ((getScreen().getHeight() - 1) / (b + c));
-        // System.err.printf("Z %s a %s b %s c %s newWidth %s newHeight1 %s newHeight2 %s",
-        //     z, a, b, c, newWidth, newHeight1, newHeight2);
-
-        List<TWindow> sorted = new LinkedList<TWindow>(windows);
-        Collections.sort(sorted);
-        Collections.reverse(sorted);
-        for (int i = 0; i < sorted.size(); i++) {
-            int logicalX = i / b;
-            int logicalY = i % b;
-            if (i >= ((a - 1) * b)) {
-                logicalX = a - 1;
-                logicalY = i - ((a - 1) * b);
+            int z = windows.size();
+            if (z == 0) {
+                return;
+            }
+            int a = 0;
+            int b = 0;
+            a = (int)(Math.sqrt(z));
+            int c = 0;
+            while (c < a) {
+                b = (z - c) / a;
+                if (((a * b) + c) == z) {
+                    break;
+                }
+                c++;
             }
+            assert (a > 0);
+            assert (b > 0);
+            assert (c < a);
+            int newWidth = (getScreen().getWidth() / a);
+            int newHeight1 = ((getScreen().getHeight() - 1) / b);
+            int newHeight2 = ((getScreen().getHeight() - 1) / (b + c));
+
+            List<TWindow> sorted = new LinkedList<TWindow>(windows);
+            Collections.sort(sorted);
+            Collections.reverse(sorted);
+            for (int i = 0; i < sorted.size(); i++) {
+                int logicalX = i / b;
+                int logicalY = i % b;
+                if (i >= ((a - 1) * b)) {
+                    logicalX = a - 1;
+                    logicalY = i - ((a - 1) * b);
+                }
 
-            TWindow w = sorted.get(i);
-            w.setX(logicalX * newWidth);
-            w.setWidth(newWidth);
-            if (i >= ((a - 1) * b)) {
-                w.setY((logicalY * newHeight2) + 1);
-                w.setHeight(newHeight2);
-            } else {
-                w.setY((logicalY * newHeight1) + 1);
-                w.setHeight(newHeight1);
+                TWindow w = sorted.get(i);
+                w.setX(logicalX * newWidth);
+                w.setWidth(newWidth);
+                if (i >= ((a - 1) * b)) {
+                    w.setY((logicalY * newHeight2) + 1);
+                    w.setHeight(newHeight2);
+                } else {
+                    w.setY((logicalY * newHeight1) + 1);
+                    w.setHeight(newHeight1);
+                }
             }
         }
     }
@@ -1403,25 +1657,27 @@ public class TApplication {
      * Re-layout the open windows as overlapping cascaded windows.
      */
     private void cascadeWindows() {
-        // Don't do anything if we are in the menu
-        if (activeMenu != null) {
-            return;
-        }
-        int x = 0;
-        int y = 1;
-        List<TWindow> sorted = new LinkedList<TWindow>(windows);
-        Collections.sort(sorted);
-        Collections.reverse(sorted);
-        for (TWindow window: sorted) {
-            window.setX(x);
-            window.setY(y);
-            x++;
-            y++;
-            if (x > getScreen().getWidth()) {
-                x = 0;
+        synchronized (windows) {
+            // Don't do anything if we are in the menu
+            if (activeMenu != null) {
+                return;
             }
-            if (y >= getScreen().getHeight()) {
-                y = 1;
+            int x = 0;
+            int y = 1;
+            List<TWindow> sorted = new LinkedList<TWindow>(windows);
+            Collections.sort(sorted);
+            Collections.reverse(sorted);
+            for (TWindow window: sorted) {
+                window.setX(x);
+                window.setY(y);
+                x++;
+                y++;
+                if (x > getScreen().getWidth()) {
+                    x = 0;
+                }
+                if (y >= getScreen().getHeight()) {
+                    y = 1;
+                }
             }
         }
     }
@@ -1513,4 +1769,29 @@ public class TApplication {
         return new TInputBox(this, title, caption, text);
     }
 
+    /**
+     * Convenience function to open a terminal window.
+     *
+     * @param x column relative to parent
+     * @param y row relative to parent
+     * @return the terminal new window
+     */
+    public final TTerminalWindow openTerminal(final int x, final int y) {
+        return openTerminal(x, y, TWindow.RESIZABLE);
+    }
+
+    /**
+     * Convenience function to open a terminal window.
+     *
+     * @param x column relative to parent
+     * @param y row relative to parent
+     * @param flags mask of CENTERED, MODAL, or RESIZABLE
+     * @return the terminal new window
+     */
+    public final TTerminalWindow openTerminal(final int x, final int y,
+        final int flags) {
+
+        return new TTerminalWindow(this, x, y, flags);
+    }
+
 }