Expose width/height in TApplication constructor, attempt on ECMA48
[nikiroo-utils.git] / src / jexer / backend / ECMA48Terminal.java
index ea99a0b79eeaf7adb24fe8c2e7a7f01b4c99f45c..ae356107ee9c05f3d049f8bbb8b969a1aea50135 100644 (file)
@@ -290,6 +290,37 @@ public final class ECMA48Terminal extends LogicalScreen
         }
     }
 
+    /**
+     * Constructor sets up state for getEvent().
+     *
+     * @param listener the object this backend needs to wake up when new
+     * input comes in
+     * @param input an InputStream connected to the remote user, or null for
+     * System.in.  If System.in is used, then on non-Windows systems it will
+     * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
+     * mode.  input is always converted to a Reader with UTF-8 encoding.
+     * @param output an OutputStream connected to the remote user, or null
+     * for System.out.  output is always converted to a Writer with UTF-8
+     * encoding.
+     * @param windowWidth the number of text columns to start with
+     * @param windowHeight the number of text rows to start with
+     * @throws UnsupportedEncodingException if an exception is thrown when
+     * creating the InputStreamReader
+     */
+    public ECMA48Terminal(final Object listener, final InputStream input,
+        final OutputStream output, final int windowWidth,
+        final int windowHeight) throws UnsupportedEncodingException {
+
+        this(listener, input, output);
+
+        // Send dtterm/xterm sequences, which will probably not work because
+        // allowWindowOps is defaulted to false.
+        String resizeString = String.format("\033[8;%d;%dt", windowHeight,
+            windowWidth);
+        this.output.write(resizeString);
+        this.output.flush();
+    }
+
     /**
      * Constructor sets up state for getEvent().
      *
@@ -1107,9 +1138,18 @@ public final class ECMA48Terminal extends LogicalScreen
             sessionInfo.queryWindowSize();
             int newWidth = sessionInfo.getWindowWidth();
             int newHeight = sessionInfo.getWindowHeight();
+
             if ((newWidth != windowResize.getWidth())
                 || (newHeight != windowResize.getHeight())
             ) {
+
+                if (debugToStderr) {
+                    System.err.println("Screen size changed, old size " +
+                        windowResize);
+                    System.err.println("                     new size " +
+                        newWidth + " x " + newHeight);
+                }
+
                 TResizeEvent event = new TResizeEvent(TResizeEvent.Type.SCREEN,
                     newWidth, newHeight);
                 windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN,
@@ -1870,14 +1910,27 @@ public final class ECMA48Terminal extends LogicalScreen
                 // We assume that if inputStream has bytes available, then
                 // input won't block on read().
                 int n = inputStream.available();
+
+                /*
+                System.err.printf("inputStream.available(): %d\n", n);
+                System.err.flush();
+                */
+
                 if (n > 0) {
                     if (readBuffer.length < n) {
                         // The buffer wasn't big enough, make it huger
                         readBuffer = new char[readBuffer.length * 2];
                     }
 
+                    // System.err.printf("BEFORE read()\n"); System.err.flush();
+
                     int rc = input.read(readBuffer, 0, readBuffer.length);
-                    // System.err.printf("read() %d", rc); System.err.flush();
+
+                    /*
+                    System.err.printf("AFTER read() %d\n", rc);
+                    System.err.flush();
+                    */
+
                     if (rc == -1) {
                         // This is EOF
                         done = true;
@@ -1907,16 +1960,16 @@ public final class ECMA48Terminal extends LogicalScreen
                         synchronized (eventQueue) {
                             eventQueue.addAll(events);
                         }
-                        events.clear();
                         if (listener != null) {
                             synchronized (listener) {
                                 listener.notifyAll();
                             }
                         }
+                        events.clear();
                     }
 
-                    // Wait 10 millis for more data
-                    Thread.sleep(10);
+                    // Wait 20 millis for more data
+                    Thread.sleep(20);
                 }
                 // System.err.println("end while loop"); System.err.flush();
             } catch (InterruptedException e) {