| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * License: LGPLv3 or later |
| 5 | * |
| 6 | * This module is licensed under the GNU Lesser General Public License |
| 7 | * Version 3. Please see the file "COPYING" in this directory for more |
| 8 | * information about the GNU Lesser General Public License Version 3. |
| 9 | * |
| 10 | * Copyright (C) 2015 Kevin Lamonte |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public License |
| 14 | * as published by the Free Software Foundation; either version 3 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, but |
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this program; if not, see |
| 24 | * http://www.gnu.org/licenses/, or write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 26 | * 02110-1301 USA |
| 27 | * |
| 28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 29 | * @version 1 |
| 30 | */ |
| 31 | package jexer.backend; |
| 32 | |
| 33 | import java.io.InputStream; |
| 34 | import java.io.OutputStream; |
| 35 | import java.io.UnsupportedEncodingException; |
| 36 | import java.util.List; |
| 37 | |
| 38 | import jexer.event.TInputEvent; |
| 39 | import jexer.io.ECMA48Screen; |
| 40 | import jexer.io.ECMA48Terminal; |
| 41 | |
| 42 | /** |
| 43 | * This class uses an xterm/ANSI X3.64/ECMA-48 type terminal to provide a |
| 44 | * screen, keyboard, and mouse to TApplication. |
| 45 | */ |
| 46 | public class ECMA48Backend extends Backend { |
| 47 | |
| 48 | /** |
| 49 | * Input events are processed by this Terminal. |
| 50 | */ |
| 51 | private ECMA48Terminal terminal; |
| 52 | |
| 53 | /** |
| 54 | * Public constructor. |
| 55 | * |
| 56 | * @param input an InputStream connected to the remote user, or null for |
| 57 | * System.in. If System.in is used, then on non-Windows systems it will |
| 58 | * be put in raw mode; shutdown() will (blindly!) put System.in in cooked |
| 59 | * mode. input is always converted to a Reader with UTF-8 encoding. |
| 60 | * @param output an OutputStream connected to the remote user, or null |
| 61 | * for System.out. output is always converted to a Writer with UTF-8 |
| 62 | * encoding. |
| 63 | * @throws UnsupportedEncodingException if an exception is thrown when |
| 64 | * creating the InputStreamReader |
| 65 | */ |
| 66 | public ECMA48Backend(final InputStream input, |
| 67 | final OutputStream output) throws UnsupportedEncodingException { |
| 68 | |
| 69 | // Create a terminal and explicitly set stdin into raw mode |
| 70 | terminal = new ECMA48Terminal(input, output); |
| 71 | |
| 72 | // Keep the terminal's sessionInfo so that TApplication can see it |
| 73 | sessionInfo = terminal.getSessionInfo(); |
| 74 | |
| 75 | // Create a screen |
| 76 | screen = new ECMA48Screen(terminal); |
| 77 | |
| 78 | // Clear the screen |
| 79 | terminal.getOutput().write(terminal.clearAll()); |
| 80 | terminal.flush(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Sync the logical screen to the physical device. |
| 85 | */ |
| 86 | @Override |
| 87 | public final void flushScreen() { |
| 88 | screen.flushPhysical(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get keyboard, mouse, and screen resize events. |
| 93 | * |
| 94 | * @param queue list to append new events to |
| 95 | * @param timeout maximum amount of time (in millis) to wait for an |
| 96 | * event. 0 means to return immediately, i.e. perform a poll. |
| 97 | */ |
| 98 | @Override |
| 99 | public void getEvents(final List<TInputEvent> queue, final int timeout) { |
| 100 | if (timeout > 0) { |
| 101 | // Try to sleep, let the terminal's input thread wake me up if |
| 102 | // something came in. |
| 103 | synchronized (terminal) { |
| 104 | try { |
| 105 | terminal.wait(timeout); |
| 106 | if (terminal.hasEvents()) { |
| 107 | // System.err.println("getEvents()"); |
| 108 | terminal.getEvents(queue); |
| 109 | } else { |
| 110 | // If I got here, then I timed out. Call |
| 111 | // terminal.getIdleEvents() to pick up stragglers |
| 112 | // like bare resize. |
| 113 | // System.err.println("getIdleEvents()"); |
| 114 | terminal.getIdleEvents(queue); |
| 115 | } |
| 116 | } catch (InterruptedException e) { |
| 117 | // Spurious interrupt, pretend it was like a timeout. |
| 118 | // System.err.println("[interrupt] getEvents()"); |
| 119 | terminal.getIdleEvents(queue); |
| 120 | } |
| 121 | } |
| 122 | } else { |
| 123 | // Asking for a poll, go get it. |
| 124 | // System.err.println("[polled] getEvents()"); |
| 125 | terminal.getEvents(queue); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Close the I/O, restore the console, etc. |
| 131 | */ |
| 132 | @Override |
| 133 | public final void shutdown() { |
| 134 | terminal.shutdown(); |
| 135 | } |
| 136 | |
| 137 | } |