retrofit from GJexer
[nikiroo-utils.git] / src / jexer / backend / TWindowBackend.java
index 7652d3f2300811f5e349916bc214f987714259f3..10f95c666da3dc8c545dbf409d8778f683fb04c8 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"),
@@ -48,6 +48,10 @@ import jexer.TWindow;
  */
 public class TWindowBackend extends TWindow implements Backend {
 
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * The listening object that run() wakes up on new input.
      */
@@ -69,47 +73,14 @@ public class TWindowBackend extends TWindow implements Backend {
      */
     private Screen otherScreen;
 
-    /**
-     * The mouse X position as seen on the other screen.
-     */
-    private int otherMouseX = -1;
-
-    /**
-     * The mouse Y position as seen on the other screen.
-     */
-    private int otherMouseY = -1;
-
     /**
      * The session information.
      */
     private SessionInfo sessionInfo;
 
-    /**
-     * Set the object to sync to in draw().
-     *
-     * @param drawLock the object to synchronize on
-     */
-    public void setDrawLock(final Object drawLock) {
-        this.drawLock = drawLock;
-    }
-
-    /**
-     * Getter for the other application's screen.
-     *
-     * @return the Screen
-     */
-    public Screen getOtherScreen() {
-        return otherScreen;
-    }
-
-    /**
-     * Getter for sessionInfo.
-     *
-     * @return the SessionInfo
-     */
-    public final SessionInfo getSessionInfo() {
-        return sessionInfo;
-    }
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Public constructor.  Window will be located at (0, 0).
@@ -133,6 +104,7 @@ public class TWindowBackend extends TWindow implements Backend {
         otherScreen = new LogicalScreen();
         otherScreen.setDimensions(width - 2, height - 2);
         drawLock = otherScreen;
+        setHiddenMouse(true);
     }
 
     /**
@@ -158,6 +130,7 @@ public class TWindowBackend extends TWindow implements Backend {
         otherScreen = new LogicalScreen();
         otherScreen.setDimensions(width - 2, height - 2);
         drawLock = otherScreen;
+        setHiddenMouse(true);
     }
 
     /**
@@ -184,6 +157,7 @@ public class TWindowBackend extends TWindow implements Backend {
         otherScreen = new LogicalScreen();
         otherScreen.setDimensions(width - 2, height - 2);
         drawLock = otherScreen;
+        setHiddenMouse(true);
     }
 
     /**
@@ -212,99 +186,12 @@ public class TWindowBackend extends TWindow implements Backend {
         otherScreen = new LogicalScreen();
         otherScreen.setDimensions(width - 2, height - 2);
         drawLock = otherScreen;
+        setHiddenMouse(true);
     }
 
-    /**
-     * Subclasses must provide an implementation that syncs the logical
-     * screen to the physical device.
-     */
-    public void flushScreen() {
-        getApplication().doRepaint();
-    }
-
-    /**
-     * Subclasses must provide an implementation to get keyboard, mouse, and
-     * screen resize events.
-     *
-     * @param queue list to append new events to
-     */
-    public void getEvents(List<TInputEvent> queue) {
-        synchronized (eventQueue) {
-            if (eventQueue.size() > 0) {
-                synchronized (queue) {
-                    queue.addAll(eventQueue);
-                }
-                eventQueue.clear();
-            }
-        }
-    }
-
-    /**
-     * Subclasses must provide an implementation that closes sockets,
-     * restores console, etc.
-     */
-    public void shutdown() {
-        // NOP
-    }
-
-    /**
-     * 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) {
-        this.listener = listener;
-    }
-
-    /**
-     * Draw the foreground colors grid.
-     */
-    @Override
-    public void draw() {
-
-        // Sync on other screen, so that we do not draw in the middle of
-        // their screen update.
-        synchronized (drawLock) {
-            // Draw the box
-            super.draw();
-
-            // Draw every cell of the other screen
-            for (int y = 0; y < otherScreen.getHeight(); y++) {
-                for (int x = 0; x < otherScreen.getWidth(); x++) {
-                    putCharXY(x + 1, y + 1, otherScreen.getCharXY(x, y));
-                }
-            }
-
-            // If the mouse pointer is over the other window, draw its
-            // pointer again here.  (Their TApplication drew it, then our
-            // TApplication drew it again (undo-ing it), so now we draw it a
-            // third time so that it is visible.)
-            if ((otherMouseX != -1) && (otherMouseY != -1)) {
-                CellAttributes attr = getAttrXY(otherMouseX, otherMouseY);
-                attr.setForeColor(attr.getForeColor().invert());
-                attr.setBackColor(attr.getBackColor().invert());
-                putAttrXY(otherMouseX, otherMouseY, attr, false);
-            }
-
-            // If their cursor is visible, draw that here too.
-            if (otherScreen.isCursorVisible()) {
-                setCursorX(otherScreen.getCursorX() + 1);
-                setCursorY(otherScreen.getCursorY() + 1);
-                setCursorVisible(true);
-            } else {
-                setCursorVisible(false);
-            }
-        }
-    }
-
-    /**
-     * Subclasses should override this method to cleanup resources.  This is
-     * called by application.closeWindow().
-     */
-    public void onClose() {
-        // TODO: send a screen disconnect
-    }
+    // ------------------------------------------------------------------------
+    // Event handlers ---------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Returns true if the mouse is currently in the otherScreen window.
@@ -382,17 +269,12 @@ public class TWindowBackend extends TWindow implements Backend {
             event.setY(mouse.getY() - 1);
             event.setAbsoluteX(event.getX());
             event.setAbsoluteY(event.getY());
-            otherMouseX = event.getX() + getX() + 1;
-            otherMouseY = event.getY() + getY() + 1;
             synchronized (eventQueue) {
                 eventQueue.add(event);
             }
             synchronized (listener) {
                 listener.notifyAll();
             }
-        } else {
-            otherMouseX = -1;
-            otherMouseY = -1;
         }
         super.onMouseMotion(mouse);
     }
@@ -413,4 +295,143 @@ public class TWindowBackend extends TWindow implements Backend {
         }
     }
 
+    // ------------------------------------------------------------------------
+    // TWindow ----------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Draw the foreground colors grid.
+     */
+    @Override
+    public void draw() {
+
+        // Sync on other screen, so that we do not draw in the middle of
+        // their screen update.
+        synchronized (drawLock) {
+            // Draw the box
+            super.draw();
+
+            // Draw every cell of the other screen
+            for (int y = 0; y < otherScreen.getHeight(); y++) {
+                for (int x = 0; x < otherScreen.getWidth(); x++) {
+                    putCharXY(x + 1, y + 1, otherScreen.getCharXY(x, y));
+                }
+            }
+
+            // If their cursor is visible, draw that here too.
+            if (otherScreen.isCursorVisible()) {
+                setCursorX(otherScreen.getCursorX() + 1);
+                setCursorY(otherScreen.getCursorY() + 1);
+                setCursorVisible(true);
+            } else {
+                setCursorVisible(false);
+            }
+        }
+    }
+
+    /**
+     * Subclasses should override this method to cleanup resources.  This is
+     * called by application.closeWindow().
+     */
+    @Override
+    public void onClose() {
+        // TODO: send a screen disconnect
+    }
+
+    // ------------------------------------------------------------------------
+    // Backend ----------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Getter for sessionInfo.
+     *
+     * @return the SessionInfo
+     */
+    public final SessionInfo getSessionInfo() {
+        return sessionInfo;
+    }
+
+    /**
+     * Subclasses must provide an implementation that syncs the logical
+     * screen to the physical device.
+     */
+    public void flushScreen() {
+        getApplication().doRepaint();
+    }
+
+    /**
+     * Check if there are events in the queue.
+     *
+     * @return if true, getEvents() has something to return to the application
+     */
+    public boolean hasEvents() {
+        synchronized (eventQueue) {
+            return (eventQueue.size() > 0);
+        }
+    }
+
+    /**
+     * Subclasses must provide an implementation to get keyboard, mouse, and
+     * screen resize events.
+     *
+     * @param queue list to append new events to
+     */
+    public void getEvents(List<TInputEvent> queue) {
+        synchronized (eventQueue) {
+            if (eventQueue.size() > 0) {
+                synchronized (queue) {
+                    queue.addAll(eventQueue);
+                }
+                eventQueue.clear();
+            }
+        }
+    }
+
+    /**
+     * Subclasses must provide an implementation that closes sockets,
+     * restores console, etc.
+     */
+    public void shutdown() {
+        // NOP
+    }
+
+    /**
+     * 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) {
+        this.listener = listener;
+    }
+
+    /**
+     * Reload backend options from System properties.
+     */
+    public void reloadOptions() {
+        // NOP
+    }
+
+    // ------------------------------------------------------------------------
+    // TWindowBackend ---------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Set the object to sync to in draw().
+     *
+     * @param drawLock the object to synchronize on
+     */
+    public void setDrawLock(final Object drawLock) {
+        this.drawLock = drawLock;
+    }
+
+    /**
+     * Getter for the other application's screen.
+     *
+     * @return the Screen
+     */
+    public Screen getOtherScreen() {
+        return otherScreen;
+    }
+
 }