support for kbTab in AWT
[nikiroo-utils.git] / src / jexer / io / AWTTerminal.java
index 6cc252f22f0a65590990ce50595a2b9d6e647f30..8b542402daf5b7321de855fbeffd8d31f09559c7 100644 (file)
@@ -81,15 +81,25 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
         return sessionInfo;
     }
 
+    /**
+     * The listening object that run() wakes up on new input.
+     */
+    private Object listener;
+
     /**
      * The event queue, filled up by a thread reading on input.
      */
     private List<TInputEvent> eventQueue;
 
     /**
-     * The reader thread.
+     * The last reported mouse X position.
      */
-    private Thread readerThread;
+    private int oldMouseX = -1;
+
+    /**
+     * The last reported mouse Y position.
+     */
+    private int oldMouseY = -1;
 
     /**
      * true if mouse1 was down.  Used to report mouse1 on the release event.
@@ -120,9 +130,12 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
     /**
      * Constructor sets up state for getEvent().
      *
+     * @param listener the object this backend needs to wake up when new
+     * input comes in
      * @param screen the top-level AWT frame
      */
-    public AWTTerminal(final AWTScreen screen) {
+    public AWTTerminal(final Object listener, final AWTScreen screen) {
+        this.listener    = listener;
         this.screen      = screen;
         mouse1           = false;
         mouse2           = false;
@@ -162,26 +175,6 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
         }
     }
 
-    /**
-     * Return any events in the IO queue due to timeout.
-     *
-     * @param queue list to append new events to
-     */
-    public void getIdleEvents(final List<TInputEvent> queue) {
-
-        // Insert any polling action here...
-
-        // Return any events that showed up
-        synchronized (eventQueue) {
-            if (eventQueue.size() > 0) {
-                synchronized (queue) {
-                    queue.addAll(eventQueue);
-                }
-                eventQueue.clear();
-            }
-        }
-    }
-
     /**
      * Pass AWT keystrokes into the event queue.
      *
@@ -214,7 +207,6 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
         boolean ctrl = false;
         char ch = ' ';
         boolean isKey = false;
-        int fnKey = 0;
         if (key.isActionKey()) {
             isKey = true;
         } else {
@@ -231,7 +223,7 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
         System.err.printf("   ctrl: %s\n", ctrl);
         System.err.printf("   shift: %s\n", shift);
         System.err.printf("   ch: %s\n", ch);
-         */
+        */
 
         // Special case: not return the bare modifier presses
         switch (key.getKeyCode()) {
@@ -380,12 +372,19 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
             case 0x0D:
                 keypress = kbEnter;
                 break;
+            case 0x09:
+                if (shift) {
+                    keypress = kbShiftTab;
+                } else {
+                    keypress = kbTab;
+                }
+                break;
             case 0x7F:
                 keypress = kbDel;
                 break;
             default:
                 if (!alt && ctrl && !shift) {
-                    ch = key.getKeyText(key.getKeyCode()).charAt(0);
+                    ch = KeyEvent.getKeyText(key.getKeyCode()).charAt(0);
                 }
                 // Not a special key, put it together
                 keypress = new TKeypress(false, 0, ch, alt, ctrl, shift);
@@ -396,9 +395,8 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
         synchronized (eventQueue) {
             eventQueue.add(new TKeypressEvent(keypress));
         }
-        // Wake up the backend
-        synchronized (this) {
-            this.notifyAll();
+        synchronized (listener) {
+            listener.notifyAll();
         }
     }
 
@@ -409,7 +407,10 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
      */
     @Override
     public void windowActivated(final WindowEvent event) {
-        // Ignore
+        // Force a total repaint
+        synchronized (screen) {
+            screen.clearPhysical();
+        }
     }
 
     /**
@@ -433,9 +434,8 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
         synchronized (eventQueue) {
             eventQueue.add(new TCommandEvent(cmAbort));
         }
-        // Wake up the backend
-        synchronized (this) {
-            this.notifyAll();
+        synchronized (listener) {
+            listener.notifyAll();
         }
     }
 
@@ -523,9 +523,8 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
                 sessionInfo.getWindowWidth(), sessionInfo.getWindowHeight());
             eventQueue.add(windowResize);
         }
-        // Wake up the backend
-        synchronized (this) {
-            this.notifyAll();
+        synchronized (listener) {
+            listener.notifyAll();
         }
     }
 
@@ -561,9 +560,8 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
         synchronized (eventQueue) {
             eventQueue.add(mouseEvent);
         }
-        // Wake up the backend
-        synchronized (this) {
-            this.notifyAll();
+        synchronized (listener) {
+            listener.notifyAll();
         }
     }
 
@@ -576,15 +574,21 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
     public void mouseMoved(final MouseEvent mouse) {
         int x = screen.textColumn(mouse.getX());
         int y = screen.textRow(mouse.getY());
+        if ((x == oldMouseX) && (y == oldMouseY)) {
+            // Bail out, we've moved some pixels but not a whole text cell.
+            return;
+        }
+        oldMouseX = x;
+        oldMouseY = y;
+
         TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_MOTION,
             x, y, x, y, mouse1, mouse2, mouse3, false, false);
 
         synchronized (eventQueue) {
             eventQueue.add(mouseEvent);
         }
-        // Wake up the backend
-        synchronized (this) {
-            this.notifyAll();
+        synchronized (listener) {
+            listener.notifyAll();
         }
     }
 
@@ -650,9 +654,8 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
         synchronized (eventQueue) {
             eventQueue.add(mouseEvent);
         }
-        // Wake up the backend
-        synchronized (this) {
-            this.notifyAll();
+        synchronized (listener) {
+            listener.notifyAll();
         }
     }
 
@@ -697,9 +700,8 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
         synchronized (eventQueue) {
             eventQueue.add(mouseEvent);
         }
-        // Wake up the backend
-        synchronized (this) {
-            this.notifyAll();
+        synchronized (listener) {
+            listener.notifyAll();
         }
     }
 
@@ -743,9 +745,8 @@ public final class AWTTerminal implements ComponentListener, KeyListener,
         synchronized (eventQueue) {
             eventQueue.add(mouseEvent);
         }
-        // Wake up the backend
-        synchronized (this) {
-            this.notifyAll();
+        synchronized (listener) {
+            listener.notifyAll();
         }
     }