#18 move to event-driven main loop
[fanfix.git] / src / jexer / tterminal / ECMA48.java
index a282058189fe0dec5eeda5a8725324879303ec71..46952da4b19066ce96b8dab768074e054e5e3665 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2016 Kevin Lamonte
+ * Copyright (C) 2017 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -28,6 +28,7 @@
  */
 package jexer.tterminal;
 
+import java.io.BufferedOutputStream;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.IOException;
@@ -301,6 +302,21 @@ public class ECMA48 implements Runnable {
         }
     }
 
+    /**
+     * The enclosing listening object.
+     */
+    private DisplayListener listener;
+
+    /**
+     * Set a listening object.
+     *
+     * @param listener the object that will have displayChanged() called
+     * after bytes are received from the remote terminal
+     */
+    public void setListener(final DisplayListener listener) {
+        this.listener = listener;
+    }
+
     /**
      * When true, the reader thread is expected to exit.
      */
@@ -905,11 +921,12 @@ public class ECMA48 implements Runnable {
         }
         if (type == DeviceType.XTERM) {
             this.input    = new InputStreamReader(this.inputStream, "UTF-8");
-            this.output   = new OutputStreamWriter(outputStream, "UTF-8");
+            this.output   = new OutputStreamWriter(new
+                BufferedOutputStream(outputStream), "UTF-8");
             this.outputStream = null;
         } else {
             this.output       = null;
-            this.outputStream = outputStream;
+            this.outputStream = new BufferedOutputStream(outputStream);
         }
 
         reset();
@@ -1796,20 +1813,9 @@ public class ECMA48 implements Runnable {
         if (keypress.equalsWithoutModifiers(kbPgUp)) {
             switch (type) {
             case XTERM:
-                switch (arrowKeyMode) {
-                case ANSI:
-                    return xtermBuildKeySequence("\033[", '5', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT52:
-                    return xtermBuildKeySequence("\033", '5', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT100:
-                    return xtermBuildKeySequence("\033O", '5', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                }
+                return xtermBuildKeySequence("\033[", '5', '~',
+                    keypress.isCtrl(), keypress.isAlt(),
+                    keypress.isShift());
             default:
                 return "\033[5~";
             }
@@ -1818,20 +1824,9 @@ public class ECMA48 implements Runnable {
         if (keypress.equalsWithoutModifiers(kbPgDn)) {
             switch (type) {
             case XTERM:
-                switch (arrowKeyMode) {
-                case ANSI:
-                    return xtermBuildKeySequence("\033[", '6', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT52:
-                    return xtermBuildKeySequence("\033", '6', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT100:
-                    return xtermBuildKeySequence("\033O", '6', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                }
+                return xtermBuildKeySequence("\033[", '6', '~',
+                    keypress.isCtrl(), keypress.isAlt(),
+                    keypress.isShift());
             default:
                 return "\033[6~";
             }
@@ -1840,20 +1835,9 @@ public class ECMA48 implements Runnable {
         if (keypress.equalsWithoutModifiers(kbIns)) {
             switch (type) {
             case XTERM:
-                switch (arrowKeyMode) {
-                case ANSI:
-                    return xtermBuildKeySequence("\033[", '2', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT52:
-                    return xtermBuildKeySequence("\033", '2', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT100:
-                    return xtermBuildKeySequence("\033O", '2', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                }
+                return xtermBuildKeySequence("\033[", '2', '~',
+                    keypress.isCtrl(), keypress.isAlt(),
+                    keypress.isShift());
             default:
                 return "\033[2~";
             }
@@ -1862,20 +1846,9 @@ public class ECMA48 implements Runnable {
         if (keypress.equalsWithoutModifiers(kbDel)) {
             switch (type) {
             case XTERM:
-                switch (arrowKeyMode) {
-                case ANSI:
-                    return xtermBuildKeySequence("\033[", '3', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT52:
-                    return xtermBuildKeySequence("\033", '3', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                case VT100:
-                    return xtermBuildKeySequence("\033O", '3', '~',
-                        keypress.isCtrl(), keypress.isAlt(),
-                        keypress.isShift());
-                }
+                return xtermBuildKeySequence("\033[", '3', '~',
+                    keypress.isCtrl(), keypress.isAlt(),
+                    keypress.isShift());
             default:
                 // Delete sends real delete for VTxxx
                 return "\177";
@@ -6066,6 +6039,10 @@ public class ECMA48 implements Runnable {
                             consume((char)ch);
                         }
                     }
+                    // Permit my enclosing UI to know that I updated.
+                    if (listener != null) {
+                        listener.displayChanged();
+                    }
                 }
                 // System.err.println("end while loop"); System.err.flush();
             } catch (IOException e) {