X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTApplication.java;h=d0c34bf0303adbe99e917f6afbcd278032915af6;hb=0d47c5460c8e9d1198928308767a63ad35f46eb8;hp=72f807818d3daae7103c586899e49260b68b9d32;hpb=e3dfbd233442a877d5efa1bc177c3d357771e5cb;p=fanfix.git diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index 72f8078..d0c34bf 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -31,6 +31,7 @@ package jexer; import java.io.InputStream; +import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Collections; @@ -174,6 +175,11 @@ public class TApplication implements Runnable { } } + // Wait for drawAll() or doIdle() to be done, then handle the + // events. + boolean oldLock = lockHandleEvent(); + assert (oldLock == false); + // Pull all events off the queue for (;;) { TInputEvent event = null; @@ -183,10 +189,6 @@ public class TApplication implements Runnable { } event = application.drainEventQueue.remove(0); } - // Wait for drawAll() or doIdle() to be done, then handle - // the event. - boolean oldLock = lockHandleEvent(); - assert (oldLock == false); application.repaint = true; if (primary) { primaryHandleEvent(event); @@ -210,14 +212,14 @@ public class TApplication implements Runnable { // All done! return; - } else { - // Unlock. Either I am primary thread, or I am - // secondary thread and still running. - oldLock = unlockHandleEvent(); - assert (oldLock == true); } } // for (;;) + // Unlock. Either I am primary thread, or I am secondary + // thread and still running. + oldLock = unlockHandleEvent(); + assert (oldLock == true); + // I have done some work of some kind. Tell the main run() // loop to wake up now. synchronized (application) { @@ -281,7 +283,14 @@ public class TApplication implements Runnable { synchronized (this) { // Wait for TApplication.run() to finish using the global state // before allowing further event processing. - while (lockoutHandleEvent == true) {} + while (lockoutHandleEvent == true) { + try { + // Backoff so that the backend can finish its work. + Thread.sleep(5); + } catch (InterruptedException e) { + // SQUASH + } + } oldValue = insideHandleEvent; insideHandleEvent = true; @@ -330,7 +339,14 @@ public class TApplication implements Runnable { lockoutHandleEvent = true; // Wait for the last event to finish processing before returning // control to TApplication.run(). - while (insideHandleEvent == true) {} + while (insideHandleEvent == true) { + try { + // Backoff so that the event handler can finish its work. + Thread.sleep(1); + } catch (InterruptedException e) { + // SQUASH + } + } if (debugThreads) { System.err.printf(" XXX\n"); @@ -575,15 +591,13 @@ public class TApplication implements Runnable { } if (!repaint) { - synchronized (getScreen()) { - if ((oldMouseX != mouseX) || (oldMouseY != mouseY)) { - // The only thing that has happened is the mouse moved. - // Clear the old position and draw the new position. - invertCell(oldMouseX, oldMouseY); - invertCell(mouseX, mouseY); - oldMouseX = mouseX; - oldMouseY = mouseY; - } + if ((oldMouseX != mouseX) || (oldMouseY != mouseY)) { + // The only thing that has happened is the mouse moved. + // Clear the old position and draw the new position. + invertCell(oldMouseX, oldMouseY); + invertCell(mouseX, mouseY); + oldMouseX = mouseX; + oldMouseY = mouseY; } if (getScreen().isDirty()) { backend.flushScreen(); @@ -633,7 +647,7 @@ public class TApplication implements Runnable { // Draw the menu title getScreen().hLineXY(x, 0, menu.getTitle().length() + 2, ' ', menuColor); - getScreen().putStrXY(x + 1, 0, menu.getTitle(), menuColor); + getScreen().putStringXY(x + 1, 0, menu.getTitle(), menuColor); // Draw the highlight character getScreen().putCharXY(x + 1 + menu.getMnemonic().getShortcutIdx(), 0, menu.getMnemonic().getShortcut(), menuMnemonicColor); @@ -696,21 +710,6 @@ public class TApplication implements Runnable { // still flip buffers reasonably quickly in // backend.flushPhysical(). timeout = getSleepTime(50); - - // 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; - } - } } if (timeout > 0) { @@ -729,31 +728,27 @@ public class TApplication implements Runnable { repaint = true; } + // Prevent stepping on the primary or secondary event handler. + stopEventHandlers(); + // Pull any pending I/O events backend.getEvents(fillEventQueue); // Dispatch each event to the appropriate handler, one at a time. for (;;) { TInputEvent event = null; - synchronized (fillEventQueue) { - if (fillEventQueue.size() == 0) { - break; - } - event = fillEventQueue.remove(0); + if (fillEventQueue.size() == 0) { + break; } + event = fillEventQueue.remove(0); metaHandleEvent(event); } // Wake a consumer thread if we have any pending events. - synchronized (drainEventQueue) { - if (drainEventQueue.size() > 0) { - wakeEventHandler(); - } + if (drainEventQueue.size() > 0) { + wakeEventHandler(); } - // Prevent stepping on the primary or secondary event handler. - stopEventHandlers(); - // Process timers and call doIdle()'s doIdle(); @@ -847,9 +842,7 @@ public class TApplication implements Runnable { } // Put into the main queue - synchronized (drainEventQueue) { - drainEventQueue.add(event); - } + drainEventQueue.add(event); } /** @@ -1828,4 +1821,30 @@ public class TApplication implements Runnable { return new TTerminalWindow(this, x, y, flags); } + /** + * Convenience function to spawn an file open box. + * + * @param path path of selected file + * @return the result of the new file open box + */ + public final String fileOpenBox(final String path) throws IOException { + + TFileOpenBox box = new TFileOpenBox(this, path, TFileOpenBox.Type.OPEN); + return box.getFilename(); + } + + /** + * Convenience function to spawn an file open box. + * + * @param path path of selected file + * @param type one of the Type constants + * @return the result of the new file open box + */ + public final String fileOpenBox(final String path, + final TFileOpenBox.Type type) throws IOException { + + TFileOpenBox box = new TFileOpenBox(this, path, type); + return box.getFilename(); + } + }