X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fio%2FECMA48Terminal.java;h=0cf0452a52f438ecc9865e66c672660cc6534c4c;hb=15ea4d7374314d856f72e83f3bc07fe9ec059741;hp=e232176be224d925c9305e10f6eb122c3cd33046;hpb=daa4106c096cd4d2b92c3cbae6491edccd25fcc4;p=fanfix.git diff --git a/src/jexer/io/ECMA48Terminal.java b/src/jexer/io/ECMA48Terminal.java index e232176..0cf0452 100644 --- a/src/jexer/io/ECMA48Terminal.java +++ b/src/jexer/io/ECMA48Terminal.java @@ -1,29 +1,27 @@ /* * Jexer - Java Text User Interface * - * License: LGPLv3 or later + * The MIT License (MIT) * - * This module is licensed under the GNU Lesser General Public License - * Version 3. Please see the file "COPYING" in this directory for more - * information about the GNU Lesser General Public License Version 3. + * Copyright (C) 2017 Kevin Lamonte * - * Copyright (C) 2015 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: * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 3 of - * the License, or (at your option) any later version. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see - * http://www.gnu.org/licenses/, or write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA + * 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 @@ -62,6 +60,12 @@ import static jexer.TKeypress.*; */ public final class ECMA48Terminal implements Runnable { + /** + * If true, emit T.416-style RGB colors. This is a) expensive in + * bandwidth, and b) potentially terrible looking for non-xterms. + */ + private static boolean doRgbColor = false; + /** * The session information. */ @@ -106,7 +110,6 @@ public final class ECMA48Terminal implements Runnable { ESCAPE_INTERMEDIATE, CSI_ENTRY, CSI_PARAM, - // CSI_INTERMEDIATE, MOUSE, MOUSE_SGR, } @@ -338,12 +341,115 @@ public final class ECMA48Terminal implements Runnable { windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN, sessionInfo.getWindowWidth(), sessionInfo.getWindowHeight()); + // Permit RGB colors only if externally requested + if (System.getProperty("jexer.ECMA48.rgbColor") != null) { + if (System.getProperty("jexer.ECMA48.rgbColor").equals("true")) { + doRgbColor = true; + } + } + // 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. + * @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) { + if (setRawMode == true) { + // Reading right off the tty + sessionInfo = new TTYSessionInfo(); + } else { + 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()); + + // Permit RGB colors only if externally requested + if (System.getProperty("jexer.ECMA48.rgbColor") != null) { + if (System.getProperty("jexer.ECMA48.rgbColor").equals("true")) { + doRgbColor = true; + } + } + + // 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. */ @@ -444,36 +550,16 @@ public final class ECMA48Terminal implements Runnable { */ private TInputEvent csiFnKey() { int key = 0; - int modifier = 0; if (params.size() > 0) { key = Integer.parseInt(params.get(0)); } - if (params.size() > 1) { - modifier = Integer.parseInt(params.get(1)); - } boolean alt = false; boolean ctrl = false; boolean shift = false; - - switch (modifier) { - case 0: - // No modifier - break; - case 2: - // Shift - shift = true; - break; - case 3: - // Alt - alt = true; - break; - case 5: - // Ctrl - ctrl = true; - break; - default: - // Unknown modifier, bail out - return null; + if (params.size() > 1) { + shift = csiIsShift(params.get(1)); + alt = csiIsAlt(params.get(1)); + ctrl = csiIsCtrl(params.get(1)); } switch (key) { @@ -779,6 +865,51 @@ public final class ECMA48Terminal implements Runnable { } } + /** + * Returns true if the CSI parameter for a keyboard command means that + * shift was down. + */ + private boolean csiIsShift(final String x) { + if ((x.equals("2")) + || (x.equals("4")) + || (x.equals("6")) + || (x.equals("8")) + ) { + return true; + } + return false; + } + + /** + * Returns true if the CSI parameter for a keyboard command means that + * alt was down. + */ + private boolean csiIsAlt(final String x) { + if ((x.equals("3")) + || (x.equals("4")) + || (x.equals("7")) + || (x.equals("8")) + ) { + return true; + } + return false; + } + + /** + * Returns true if the CSI parameter for a keyboard command means that + * ctrl was down. + */ + private boolean csiIsCtrl(final String x) { + if ((x.equals("5")) + || (x.equals("6")) + || (x.equals("7")) + || (x.equals("8")) + ) { + return true; + } + return false; + } + /** * Parses the next character of input to see if an InputEvent is * fully here. @@ -906,65 +1037,21 @@ public final class ECMA48Terminal implements Runnable { switch (ch) { case 'A': // Up - if (params.size() > 1) { - if (params.get(1).equals("2")) { - shift = true; - } - if (params.get(1).equals("5")) { - ctrl = true; - } - if (params.get(1).equals("3")) { - alt = true; - } - } events.add(new TKeypressEvent(kbUp, alt, ctrl, shift)); reset(); return; case 'B': // Down - if (params.size() > 1) { - if (params.get(1).equals("2")) { - shift = true; - } - if (params.get(1).equals("5")) { - ctrl = true; - } - if (params.get(1).equals("3")) { - alt = true; - } - } events.add(new TKeypressEvent(kbDown, alt, ctrl, shift)); reset(); return; case 'C': // Right - if (params.size() > 1) { - if (params.get(1).equals("2")) { - shift = true; - } - if (params.get(1).equals("5")) { - ctrl = true; - } - if (params.get(1).equals("3")) { - alt = true; - } - } events.add(new TKeypressEvent(kbRight, alt, ctrl, shift)); reset(); return; case 'D': // Left - if (params.size() > 1) { - if (params.get(1).equals("2")) { - shift = true; - } - if (params.get(1).equals("5")) { - ctrl = true; - } - if (params.get(1).equals("3")) { - alt = true; - } - } events.add(new TKeypressEvent(kbLeft, alt, ctrl, shift)); reset(); return; @@ -1063,15 +1150,9 @@ public final class ECMA48Terminal implements Runnable { case 'A': // Up if (params.size() > 1) { - if (params.get(1).equals("2")) { - shift = true; - } - if (params.get(1).equals("5")) { - ctrl = true; - } - if (params.get(1).equals("3")) { - alt = true; - } + shift = csiIsShift(params.get(1)); + alt = csiIsAlt(params.get(1)); + ctrl = csiIsCtrl(params.get(1)); } events.add(new TKeypressEvent(kbUp, alt, ctrl, shift)); reset(); @@ -1079,15 +1160,9 @@ public final class ECMA48Terminal implements Runnable { case 'B': // Down if (params.size() > 1) { - if (params.get(1).equals("2")) { - shift = true; - } - if (params.get(1).equals("5")) { - ctrl = true; - } - if (params.get(1).equals("3")) { - alt = true; - } + shift = csiIsShift(params.get(1)); + alt = csiIsAlt(params.get(1)); + ctrl = csiIsCtrl(params.get(1)); } events.add(new TKeypressEvent(kbDown, alt, ctrl, shift)); reset(); @@ -1095,15 +1170,9 @@ public final class ECMA48Terminal implements Runnable { case 'C': // Right if (params.size() > 1) { - if (params.get(1).equals("2")) { - shift = true; - } - if (params.get(1).equals("5")) { - ctrl = true; - } - if (params.get(1).equals("3")) { - alt = true; - } + shift = csiIsShift(params.get(1)); + alt = csiIsAlt(params.get(1)); + ctrl = csiIsCtrl(params.get(1)); } events.add(new TKeypressEvent(kbRight, alt, ctrl, shift)); reset(); @@ -1111,19 +1180,33 @@ public final class ECMA48Terminal implements Runnable { case 'D': // Left if (params.size() > 1) { - if (params.get(1).equals("2")) { - shift = true; - } - if (params.get(1).equals("5")) { - ctrl = true; - } - if (params.get(1).equals("3")) { - alt = true; - } + shift = csiIsShift(params.get(1)); + alt = csiIsAlt(params.get(1)); + ctrl = csiIsCtrl(params.get(1)); } events.add(new TKeypressEvent(kbLeft, alt, ctrl, shift)); reset(); return; + case 'H': + // Home + if (params.size() > 1) { + shift = csiIsShift(params.get(1)); + alt = csiIsAlt(params.get(1)); + ctrl = csiIsCtrl(params.get(1)); + } + events.add(new TKeypressEvent(kbHome, alt, ctrl, shift)); + reset(); + return; + case 'F': + // End + if (params.size() > 1) { + shift = csiIsShift(params.get(1)); + alt = csiIsAlt(params.get(1)); + ctrl = csiIsCtrl(params.get(1)); + } + events.add(new TKeypressEvent(kbEnd, alt, ctrl, shift)); + reset(); + return; default: break; } @@ -1165,17 +1248,114 @@ public final class ECMA48Terminal implements Runnable { return "\033[?1036l"; } + /** + * Create an xterm OSC sequence to change the window title. Note package + * private access. + * + * @param title the new title + * @return the string to emit to xterm + */ + String setTitle(final String title) { + return "\033]2;" + title + "\007"; + } + /** * Create a SGR parameter sequence for a single color change. Note * package private access. * + * @param bold if true, set bold * @param color one of the Color.WHITE, Color.BLUE, etc. constants * @param foreground if true, this is a foreground color * @return the string to emit to an ANSI / ECMA-style terminal, * e.g. "\033[42m" */ - String color(final Color color, final boolean foreground) { - return color(color, foreground, true); + String color(final boolean bold, final Color color, + final boolean foreground) { + return color(color, foreground, true) + + rgbColor(bold, color, foreground); + } + + /** + * Create a T.416 RGB parameter sequence for a single color change. + * + * @param bold if true, set bold + * @param color one of the Color.WHITE, Color.BLUE, etc. constants + * @param foreground if true, this is a foreground color + * @return the string to emit to an xterm terminal with RGB support, + * e.g. "\033[38;2;RR;GG;BBm" + */ + private String rgbColor(final boolean bold, final Color color, + final boolean foreground) { + if (doRgbColor == false) { + return ""; + } + StringBuilder sb = new StringBuilder("\033["); + if (bold) { + // Bold implies foreground only + sb.append("38;2;"); + if (color.equals(Color.BLACK)) { + sb.append("116;116;116"); + } else if (color.equals(Color.RED)) { + sb.append("252;116;116"); + } else if (color.equals(Color.GREEN)) { + sb.append("116;252;116"); + } else if (color.equals(Color.YELLOW)) { + sb.append("252;252;116"); + } else if (color.equals(Color.BLUE)) { + sb.append("116;116;252"); + } else if (color.equals(Color.MAGENTA)) { + sb.append("252;116;252"); + } else if (color.equals(Color.CYAN)) { + sb.append("116;252;252"); + } else if (color.equals(Color.WHITE)) { + sb.append("252;252;252"); + } + } else { + if (foreground) { + sb.append("38;2;"); + } else { + sb.append("48;2;"); + } + if (color.equals(Color.BLACK)) { + sb.append("0;0;0"); + } else if (color.equals(Color.RED)) { + sb.append("168;0;0"); + } else if (color.equals(Color.GREEN)) { + sb.append("0;168;0"); + } else if (color.equals(Color.YELLOW)) { + sb.append("168;116;0"); + } else if (color.equals(Color.BLUE)) { + sb.append("0;0;168"); + } else if (color.equals(Color.MAGENTA)) { + sb.append("168;0;168"); + } else if (color.equals(Color.CYAN)) { + sb.append("0;168;168"); + } else if (color.equals(Color.WHITE)) { + sb.append("168;168;168"); + } + } + sb.append("m"); + return sb.toString(); + } + + /** + * Create a T.416 RGB parameter sequence for both foreground and + * background color change. + * + * @param bold if true, set bold + * @param foreColor one of the Color.WHITE, Color.BLUE, etc. constants + * @param backColor one of the Color.WHITE, Color.BLUE, etc. constants + * @return the string to emit to an xterm terminal with RGB support, + * e.g. "\033[38;2;RR;GG;BB;48;2;RR;GG;BBm" + */ + private String rgbColor(final boolean bold, final Color foreColor, + final Color backColor) { + if (doRgbColor == false) { + return ""; + } + + return rgbColor(bold, foreColor, true) + + rgbColor(false, backColor, false); } /** @@ -1211,13 +1391,16 @@ public final class ECMA48Terminal implements Runnable { * Create a SGR parameter sequence for both foreground and background * color change. Note package private access. * + * @param bold if true, set bold * @param foreColor one of the Color.WHITE, Color.BLUE, etc. constants * @param backColor one of the Color.WHITE, Color.BLUE, etc. constants * @return the string to emit to an ANSI / ECMA-style terminal, * e.g. "\033[31;42m" */ - String color(final Color foreColor, final Color backColor) { - return color(foreColor, backColor, true); + String color(final boolean bold, final Color foreColor, + final Color backColor) { + return color(foreColor, backColor, true) + + rgbColor(bold, foreColor, backColor); } /** @@ -1310,7 +1493,7 @@ public final class ECMA48Terminal implements Runnable { sb.append("\033[0;"); } sb.append(String.format("%d;%dm", ecmaForeColor, ecmaBackColor)); - return sb.toString(); + return sb.toString() + rgbColor(bold, foreColor, backColor); } /** @@ -1406,9 +1589,9 @@ public final class ECMA48Terminal implements Runnable { */ private String mouse(final boolean on) { if (on) { - return "\033[?1003;1005;1006h\033[?1049h"; + return "\033[?1002;1003;1005;1006h\033[?1049h"; } - return "\033[?1003;1006;1005l\033[?1049l"; + return "\033[?1002;1003;1006;1005l\033[?1049l"; } /**