+public class TApplication implements Runnable {
+
+ /**
+ * 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;
+
+ /**
+ * Two backend types are available.
+ */
+ public static enum BackendType {
+ /**
+ * A Swing JFrame.
+ */
+ SWING,
+
+ /**
+ * An ECMA48 / ANSI X3.64 / XTERM style terminal.
+ */
+ ECMA48,
+
+ /**
+ * Synonym for ECMA48.
+ */
+ XTERM
+ }
+
+ /**
+ * WidgetEventHandler is the main event consumer loop. There are at most
+ * two such threads in existence: the primary for normal case and a
+ * secondary that is used for TMessageBox, TInputBox, and similar.
+ */
+ private class WidgetEventHandler implements Runnable {
+ /**
+ * The main application.
+ */
+ private TApplication application;
+
+ /**
+ * Whether or not this WidgetEventHandler is the primary or secondary
+ * thread.
+ */
+ private boolean primary = true;
+
+ /**
+ * Public constructor.
+ *
+ * @param application the main application
+ * @param primary if true, this is the primary event handler thread
+ */
+ public WidgetEventHandler(final TApplication application,
+ final boolean primary) {
+
+ this.application = application;
+ this.primary = primary;
+ }
+
+ /**
+ * The consumer loop.
+ */
+ public void run() {
+
+ // Loop forever
+ while (!application.quit) {
+
+ // Wait until application notifies me
+ while (!application.quit) {
+ try {
+ synchronized (application.drainEventQueue) {
+ if (application.drainEventQueue.size() > 0) {
+ break;
+ }
+ }
+
+ synchronized (this) {
+ if (debugThreads) {
+ System.err.printf("%s %s sleep\n", this,
+ primary ? "primary" : "secondary");
+ }
+
+ this.wait();
+
+ if (debugThreads) {
+ System.err.printf("%s %s AWAKE\n", this,
+ primary ? "primary" : "secondary");
+ }
+
+ if ((!primary)
+ && (application.secondaryEventReceiver == null)
+ ) {
+ // Secondary thread, emergency exit. If we
+ // got here then something went wrong with
+ // the handoff between yield() and
+ // closeWindow().
+ synchronized (application.primaryEventHandler) {
+ application.primaryEventHandler.notify();
+ }
+ application.secondaryEventHandler = null;
+ throw new RuntimeException(
+ "secondary exited at wrong time");
+ }
+ break;
+ }
+ } catch (InterruptedException e) {
+ // SQUASH
+ }
+ }
+
+ // 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;
+ synchronized (application.drainEventQueue) {
+ if (application.drainEventQueue.size() == 0) {
+ break;
+ }
+ event = application.drainEventQueue.remove(0);
+ }
+ application.repaint = true;
+ if (primary) {
+ primaryHandleEvent(event);
+ } else {
+ secondaryHandleEvent(event);
+ }
+ if ((!primary)
+ && (application.secondaryEventReceiver == null)
+ ) {
+ // 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;
+ }
+ } // 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) {
+ application.notify();
+ }
+
+ } // while (true) (main runnable loop)
+ }
+ }
+
+ /**
+ * The primary event handler thread.
+ */
+ private volatile WidgetEventHandler primaryEventHandler;
+
+ /**
+ * The secondary event handler thread.
+ */
+ private volatile WidgetEventHandler secondaryEventHandler;
+
+ /**
+ * The widget receiving events from the secondary event handler thread.
+ */
+ 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) {
+ try {
+ // Backoff so that the backend can finish its work.
+ Thread.sleep(5);
+ } catch (InterruptedException e) {
+ // SQUASH
+ }
+ }
+
+ 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) {
+ 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");
+ }
+ }
+
+ /**
+ * 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;
+ }