misc fixes
[fanfix.git] / src / jexer / backend / GenericBackend.java
index d96f7a93f468afc404a61888faf62bfcef337d7a..fa72956c8e27c6c247005816ff8f3489b505d070 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -39,11 +39,33 @@ import jexer.event.TInputEvent;
  */
 public abstract class GenericBackend implements Backend {
 
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * The session information.
      */
     protected SessionInfo sessionInfo;
 
+    /**
+     * The screen to draw on.
+     */
+    protected Screen screen;
+
+    /**
+     * Input events are processed by this Terminal.
+     */
+    protected TerminalReader terminal;
+
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    // ------------------------------------------------------------------------
+    // Backend ----------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Getter for sessionInfo.
      *
@@ -53,11 +75,6 @@ public abstract class GenericBackend implements Backend {
         return sessionInfo;
     }
 
-    /**
-     * The screen to draw on.
-     */
-    protected Screen screen;
-
     /**
      * Getter for screen.
      *
@@ -68,30 +85,63 @@ public abstract class GenericBackend implements Backend {
     }
 
     /**
-     * Subclasses must provide an implementation that syncs the logical
-     * screen to the physical device.
+     * Sync the logical screen to the physical device.
+     */
+    public void flushScreen() {
+        screen.flushPhysical();
+    }
+
+    /**
+     * Check if there are events in the queue.
+     *
+     * @return if true, getEvents() has something to return to the application
      */
-    public abstract void flushScreen();
+    public boolean hasEvents() {
+        return terminal.hasEvents();
+    }
 
     /**
-     * Subclasses must provide an implementation to get keyboard, mouse, and
-     * screen resize events.
+     * Get keyboard, mouse, and screen resize events.
      *
      * @param queue list to append new events to
      */
-    public abstract void getEvents(List<TInputEvent> queue);
+    public void getEvents(final List<TInputEvent> queue) {
+        if (terminal.hasEvents()) {
+            terminal.getEvents(queue);
+        }
+    }
 
     /**
-     * Subclasses must provide an implementation that closes sockets,
-     * restores console, etc.
+     * Close the I/O, restore the console, etc.
      */
-    public abstract void shutdown();
+    public void shutdown() {
+        terminal.closeTerminal();
+    }
 
     /**
-     * Subclasses must provide an implementation that sets the window title.
+     * Set the window title.
      *
      * @param title the new title
      */
-    public abstract void setTitle(final String title);
+    public void setTitle(final String title) {
+        screen.setTitle(title);
+    }
+
+    /**
+     * Set listener to a different Object.
+     *
+     * @param listener the new listening object that run() wakes up on new
+     * input
+     */
+    public void setListener(final Object listener) {
+        terminal.setListener(listener);
+    }
+
+    /**
+     * Reload backend options from System properties.
+     */
+    public void reloadOptions() {
+        terminal.reloadOptions();
+    }
 
 }