Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / jexer / backend / GenericBackend.java
index fa72956c8e27c6c247005816ff8f3489b505d070..ede3c0bff4365743f1d8c38d3c4b130f1cdc01f0 100644 (file)
@@ -31,6 +31,8 @@ package jexer.backend;
 import java.util.List;
 
 import jexer.event.TInputEvent;
+import jexer.event.TCommandEvent;
+import static jexer.TCommand.*;
 
 /**
  * This abstract class provides a screen, keyboard, and mouse to
@@ -58,6 +60,16 @@ public abstract class GenericBackend implements Backend {
      */
     protected TerminalReader terminal;
 
+    /**
+     * By default, GenericBackend adds a cmAbort after it sees
+     * cmBackendDisconnect, so that TApplication will exit when the user
+     * closes the Swing window or disconnects the ECMA48 streams.  But
+     * MultiBackend wraps multiple Backends, and needs to decide when to send
+     * cmAbort differently.  Setting this to false is how it manages that.
+     * Note package private access.
+     */
+    boolean abortOnDisconnect = true;
+
     // ------------------------------------------------------------------------
     // Constructors -----------------------------------------------------------
     // ------------------------------------------------------------------------
@@ -108,6 +120,18 @@ public abstract class GenericBackend implements Backend {
     public void getEvents(final List<TInputEvent> queue) {
         if (terminal.hasEvents()) {
             terminal.getEvents(queue);
+
+            // This default backend assumes a single user, and if that user
+            // becomes disconnected we should terminate the application.
+            if ((queue.size() > 0) && (abortOnDisconnect == true)) {
+                TInputEvent event = queue.get(queue.size() - 1);
+                if (event instanceof TCommandEvent) {
+                    TCommandEvent command = (TCommandEvent) event;
+                    if (command.equals(cmBackendDisconnect)) {
+                        queue.add(new TCommandEvent(cmAbort));
+                    }
+                }
+            }
         }
     }