From: Kevin Lamonte Date: Thu, 2 Feb 2017 01:14:59 +0000 (-0500) Subject: Expose ECMA48 Reader and Writer X-Git-Tag: fanfix-3.0.1^2~290 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=6985c57265a980ceeb8cb10a8677fe387adb3eb7;p=fanfix.git Expose ECMA48 Reader and Writer --- diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index f4cf2d5..c43f436 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -31,6 +31,8 @@ package jexer; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.Reader; import java.io.UnsupportedEncodingException; import java.util.Collections; import java.util.Date; @@ -544,6 +546,40 @@ public class TApplication implements Runnable { TApplicationImpl(); } + /** + * Public constructor. The backend type will be BackendType.ECMA48. + * + * @param input the InputStream underlying 'reader'. Its available() + * method is used to determine if reader.read() will block or not. + * @param reader a Reader connected to the remote user. + * @param writer a PrintWriter connected to the remote user. + * @param setRawMode if true, set System.in into raw mode with stty. + * This should in general not be used. It is here solely for Demo3, + * which uses System.in. + * @throws IllegalArgumentException if input, reader, or writer are null. + */ + public TApplication(final InputStream input, final Reader reader, + final PrintWriter writer, final boolean setRawMode) { + + backend = new ECMA48Backend(this, input, reader, writer, setRawMode); + TApplicationImpl(); + } + + /** + * Public constructor. The backend type will be BackendType.ECMA48. + * + * @param input the InputStream underlying 'reader'. Its available() + * method is used to determine if reader.read() will block or not. + * @param reader a Reader connected to the remote user. + * @param writer a PrintWriter connected to the remote user. + * @throws IllegalArgumentException if input, reader, or writer are null. + */ + public TApplication(final InputStream input, final Reader reader, + final PrintWriter writer) { + + this(input, reader, writer, false); + } + /** * Public constructor. This hook enables use with new non-Jexer * backends. diff --git a/src/jexer/backend/ECMA48Backend.java b/src/jexer/backend/ECMA48Backend.java index 56e4ee9..62fae34 100644 --- a/src/jexer/backend/ECMA48Backend.java +++ b/src/jexer/backend/ECMA48Backend.java @@ -30,6 +30,8 @@ package jexer.backend; import java.io.InputStream; import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.Reader; import java.io.UnsupportedEncodingException; import java.util.List; @@ -80,6 +82,56 @@ public final class ECMA48Backend extends Backend { terminal.flush(); } + /** + * Public constructor. + * + * @param listener the object this backend needs to wake up when new + * input comes in + * @param input the InputStream underlying 'reader'. Its available() + * method is used to determine if reader.read() will block or not. + * @param reader a Reader connected to the remote user. + * @param writer a PrintWriter connected to the remote user. + * @param setRawMode if true, set System.in into raw mode with stty. + * This should in general not be used. It is here solely for Demo3, + * which uses System.in. + * @throws IllegalArgumentException if input, reader, or writer are null. + */ + public ECMA48Backend(final Object listener, final InputStream input, + final Reader reader, final PrintWriter writer, + final boolean setRawMode) { + + // Create a terminal and explicitly set stdin into raw mode + terminal = new ECMA48Terminal(listener, input, reader, writer, + setRawMode); + + // Keep the terminal's sessionInfo so that TApplication can see it + sessionInfo = terminal.getSessionInfo(); + + // Create a screen + screen = new ECMA48Screen(terminal); + + // Clear the screen + terminal.getOutput().write(terminal.clearAll()); + terminal.flush(); + } + + /** + * Public constructor. + * + * @param listener the object this backend needs to wake up when new + * input comes in + * @param input the InputStream underlying 'reader'. Its available() + * method is used to determine if reader.read() will block or not. + * @param reader a Reader connected to the remote user. + * @param writer a PrintWriter connected to the remote user. + * @throws IllegalArgumentException if input, reader, or writer are null. + */ + public ECMA48Backend(final Object listener, final InputStream input, + final Reader reader, final PrintWriter writer) { + + this(listener, input, reader, writer, false); + } + /** * Sync the logical screen to the physical device. */ diff --git a/src/jexer/demos/Demo3.java b/src/jexer/demos/Demo3.java new file mode 100644 index 0000000..b8181bd --- /dev/null +++ b/src/jexer/demos/Demo3.java @@ -0,0 +1,61 @@ +/* + * Jexer - Java Text User Interface + * + * The MIT License (MIT) + * + * Copyright (C) 2016 Kevin Lamonte + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * @author Kevin Lamonte [kevin.lamonte@gmail.com] + * @version 1 + */ +package jexer.demos; + +import java.io.*; + +/** + * This class is the main driver for a simple demonstration of Jexer's + * capabilities. This one passes separate Reader/Writer to TApplication, + * which will behave quite badly due to System.in/out not being in raw mode. + */ +public class Demo3 { + + /** + * Main entry point. + * + * @param args Command line arguments + */ + public static void main(final String [] args) { + try { + if (args.length == 0) { + System.err.printf("USAGE: java -cp jexer.jar jexer.demos.Demo3\n"); + return; + } + DemoApplication app = new DemoApplication(System.in, + new InputStreamReader(System.in, "UTF-8"), + new PrintWriter(new OutputStreamWriter(System.out, "UTF-8")), + true); + (new Thread(app)).start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/src/jexer/demos/DemoApplication.java b/src/jexer/demos/DemoApplication.java index 6dad95b..7e7126f 100644 --- a/src/jexer/demos/DemoApplication.java +++ b/src/jexer/demos/DemoApplication.java @@ -95,6 +95,39 @@ public class DemoApplication extends TApplication { addAllWidgets(); } + /** + * Public constructor. + * + * @param input the InputStream underlying 'reader'. Its available() + * method is used to determine if reader.read() will block or not. + * @param reader a Reader connected to the remote user. + * @param writer a PrintWriter connected to the remote user. + * @param setRawMode if true, set System.in into raw mode with stty. + * This should in general not be used. It is here solely for Demo3, + * which uses System.in. + * @throws IllegalArgumentException if input, reader, or writer are null. + */ + public DemoApplication(final InputStream input, final Reader reader, + final PrintWriter writer, final boolean setRawMode) { + super(input, reader, writer, setRawMode); + addAllWidgets(); + } + + /** + * Public constructor. + * + * @param input the InputStream underlying 'reader'. Its available() + * method is used to determine if reader.read() will block or not. + * @param reader a Reader connected to the remote user. + * @param writer a PrintWriter connected to the remote user. + * @throws IllegalArgumentException if input, reader, or writer are null. + */ + public DemoApplication(final InputStream input, final Reader reader, + final PrintWriter writer) { + + this(input, reader, writer, false); + } + /** * Handle menu events. * diff --git a/src/jexer/io/ECMA48Terminal.java b/src/jexer/io/ECMA48Terminal.java index 069c143..a400ba2 100644 --- a/src/jexer/io/ECMA48Terminal.java +++ b/src/jexer/io/ECMA48Terminal.java @@ -341,6 +341,90 @@ public final class ECMA48Terminal implements Runnable { readerThread.start(); } + /** + * Constructor sets up state for getEvent(). + * + * @param listener the object this backend needs to wake up when new + * input comes in + * @param input the InputStream underlying 'reader'. Its available() + * method is used to determine if reader.read() will block or not. + * @param reader a Reader connected to the remote user. + * @param writer a PrintWriter connected to the remote user. + * @param setRawMode if true, set System.in into raw mode with stty. + * This should in general not be used. It is here solely for Demo3, + * which uses System.in. + * @throws IllegalArgumentException if input, reader, or writer are null. + */ + public ECMA48Terminal(final Object listener, final InputStream input, + final Reader reader, final PrintWriter writer, + final boolean setRawMode) { + + if (input == null) { + throw new IllegalArgumentException("InputStream must be specified"); + } + if (reader == null) { + throw new IllegalArgumentException("Reader must be specified"); + } + if (writer == null) { + throw new IllegalArgumentException("Writer must be specified"); + } + reset(); + mouse1 = false; + mouse2 = false; + mouse3 = false; + stopReaderThread = false; + this.listener = listener; + + inputStream = input; + this.input = reader; + + if (setRawMode == true) { + sttyRaw(); + } + this.setRawMode = setRawMode; + + if (input instanceof SessionInfo) { + // This is a TelnetInputStream that exposes window size and + // environment variables from the telnet layer. + sessionInfo = (SessionInfo) input; + } + if (sessionInfo == null) { + sessionInfo = new TSessionInfo(); + } + + this.output = writer; + + // Enable mouse reporting and metaSendsEscape + this.output.printf("%s%s", mouse(true), xtermMetaSendsEscape(true)); + this.output.flush(); + + // Hang onto the window size + windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN, + sessionInfo.getWindowWidth(), sessionInfo.getWindowHeight()); + + // Spin up the input reader + eventQueue = new LinkedList(); + readerThread = new Thread(this); + readerThread.start(); + } + + /** + * Constructor sets up state for getEvent(). + * + * @param listener the object this backend needs to wake up when new + * input comes in + * @param input the InputStream underlying 'reader'. Its available() + * method is used to determine if reader.read() will block or not. + * @param reader a Reader connected to the remote user. + * @param writer a PrintWriter connected to the remote user. + * @throws IllegalArgumentException if input, reader, or writer are null. + */ + public ECMA48Terminal(final Object listener, final InputStream input, + final Reader reader, final PrintWriter writer) { + + this(listener, input, reader, writer, false); + } + /** * Restore terminal to normal state. */