misc cleanup
[fanfix.git] / src / jexer / backend / TWindowBackend.java
index 41809cff5c5cd0452b8d9d508de8c02fe9d3bfb9..ed701ad0076dc93d2a9a54f66254f70d2bd0204f 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"),
@@ -31,10 +31,10 @@ package jexer.backend;
 import java.util.LinkedList;
 import java.util.List;
 
-import jexer.bits.CellAttributes;
 import jexer.event.TInputEvent;
 import jexer.event.TKeypressEvent;
 import jexer.event.TMouseEvent;
+import jexer.event.TResizeEvent;
 import jexer.TApplication;
 import jexer.TWindow;
 
@@ -69,24 +69,49 @@ public class TWindowBackend extends TWindow implements Backend {
     private List<TInputEvent> eventQueue;
 
     /**
-     * The screen to use.
+     * The screen this window is monitoring.
      */
     private Screen otherScreen;
 
     /**
-     * The mouse X position as seen on the other screen.
+     * The application associated with otherScreen.
      */
-    private int otherMouseX = -1;
+    private TApplication otherApplication;
 
     /**
-     * The mouse Y position as seen on the other screen.
+     * The session information.
      */
-    private int otherMouseY = -1;
+    private SessionInfo sessionInfo;
 
     /**
-     * The session information.
+     * OtherScreen provides a hook to notify TWindowBackend of screen size
+     * changes.
      */
-    private SessionInfo sessionInfo;
+    private class OtherScreen extends LogicalScreen {
+
+        /**
+         * The TWindowBackend to notify.
+         */
+        private TWindowBackend window;
+
+        /**
+         * Public constructor.
+         */
+        public OtherScreen(final TWindowBackend window) {
+            this.window = window;
+        }
+
+        /**
+         * Resize the physical screen to match the logical screen dimensions.
+         */
+        @Override
+        public void resizeToScreen() {
+            window.setWidth(getWidth() + 2);
+            window.setHeight(getHeight() + 2);
+        }
+
+    }
+
 
     // ------------------------------------------------------------------------
     // Constructors -----------------------------------------------------------
@@ -111,9 +136,10 @@ public class TWindowBackend extends TWindow implements Backend {
         this.listener = listener;
         eventQueue = new LinkedList<TInputEvent>();
         sessionInfo = new TSessionInfo(width, height);
-        otherScreen = new LogicalScreen();
+        otherScreen = new OtherScreen(this);
         otherScreen.setDimensions(width - 2, height - 2);
         drawLock = otherScreen;
+        setHiddenMouse(true);
     }
 
     /**
@@ -136,9 +162,10 @@ public class TWindowBackend extends TWindow implements Backend {
         this.listener = listener;
         eventQueue = new LinkedList<TInputEvent>();
         sessionInfo = new TSessionInfo(width, height);
-        otherScreen = new LogicalScreen();
+        otherScreen = new OtherScreen(this);
         otherScreen.setDimensions(width - 2, height - 2);
         drawLock = otherScreen;
+        setHiddenMouse(true);
     }
 
     /**
@@ -162,9 +189,10 @@ public class TWindowBackend extends TWindow implements Backend {
         this.listener = listener;
         eventQueue = new LinkedList<TInputEvent>();
         sessionInfo = new TSessionInfo(width, height);
-        otherScreen = new LogicalScreen();
+        otherScreen = new OtherScreen(this);
         otherScreen.setDimensions(width - 2, height - 2);
         drawLock = otherScreen;
+        setHiddenMouse(true);
     }
 
     /**
@@ -190,15 +218,45 @@ public class TWindowBackend extends TWindow implements Backend {
         this.listener = listener;
         eventQueue = new LinkedList<TInputEvent>();
         sessionInfo = new TSessionInfo(width, height);
-        otherScreen = new LogicalScreen();
+        otherScreen = new OtherScreen(this);
         otherScreen.setDimensions(width - 2, height - 2);
         drawLock = otherScreen;
+        setHiddenMouse(true);
     }
 
     // ------------------------------------------------------------------------
     // Event handlers ---------------------------------------------------------
     // ------------------------------------------------------------------------
 
+    /**
+     * Handle window/screen resize events.
+     *
+     * @param event resize event
+     */
+    @Override
+    public void onResize(final TResizeEvent event) {
+        if (event.getType() == TResizeEvent.Type.WIDGET) {
+            int newWidth = event.getWidth() - 2;
+            int newHeight = event.getHeight() - 2;
+            if ((newWidth != otherScreen.getWidth())
+                || (newHeight != otherScreen.getHeight())
+            ) {
+                // I was resized, notify the screen I am watching to match my
+                // new size.
+                synchronized (eventQueue) {
+                    eventQueue.add(new TResizeEvent(TResizeEvent.Type.SCREEN,
+                            newWidth, newHeight));
+                }
+                synchronized (listener) {
+                    listener.notifyAll();
+                }
+            }
+            return;
+        } else {
+            super.onResize(event);
+        }
+    }
+
     /**
      * Returns true if the mouse is currently in the otherScreen window.
      *
@@ -275,17 +333,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);
     }
@@ -329,17 +382,6 @@ public class TWindowBackend extends TWindow implements Backend {
                 }
             }
 
-            // 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);
@@ -349,6 +391,15 @@ public class TWindowBackend extends TWindow implements Backend {
                 setCursorVisible(false);
             }
         }
+
+        // Check if the other application has died.  If so, unset hidden
+        // mouse.
+        if (otherApplication != null) {
+            if (otherApplication.isRunning() == false) {
+                setHiddenMouse(false);
+            }
+        }
+
     }
 
     /**
@@ -427,6 +478,13 @@ public class TWindowBackend extends TWindow implements Backend {
         this.listener = listener;
     }
 
+    /**
+     * Reload backend options from System properties.
+     */
+    public void reloadOptions() {
+        // NOP
+    }
+
     // ------------------------------------------------------------------------
     // TWindowBackend ---------------------------------------------------------
     // ------------------------------------------------------------------------
@@ -449,4 +507,13 @@ public class TWindowBackend extends TWindow implements Backend {
         return otherScreen;
     }
 
+    /**
+     * Set the other screen's application.
+     *
+     * @param application the application driving the other screen
+     */
+    public void setOtherApplication(final TApplication application) {
+        this.otherApplication = application;
+    }
+
 }