| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * Version: $Id$ |
| 5 | * |
| 6 | * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a> |
| 7 | * |
| 8 | * License: LGPLv3 or later |
| 9 | * |
| 10 | * Copyright: This module is licensed under the GNU Lesser General |
| 11 | * Public License Version 3. Please see the file "COPYING" in this |
| 12 | * directory for more information about the GNU Lesser General Public |
| 13 | * License Version 3. |
| 14 | * |
| 15 | * Copyright (C) 2015 Kevin Lamonte |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU Lesser General Public License |
| 19 | * as published by the Free Software Foundation; either version 3 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, but |
| 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | * General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU Lesser General Public |
| 28 | * License along with this program; if not, see |
| 29 | * http://www.gnu.org/licenses/, or write to the Free Software |
| 30 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 31 | * 02110-1301 USA |
| 32 | */ |
| 33 | package jexer.backend; |
| 34 | |
| 35 | import java.io.InputStream; |
| 36 | import java.io.OutputStream; |
| 37 | import java.io.UnsupportedEncodingException; |
| 38 | import java.util.List; |
| 39 | |
| 40 | import jexer.event.TInputEvent; |
| 41 | import jexer.io.ECMA48Screen; |
| 42 | import jexer.io.ECMA48Terminal; |
| 43 | |
| 44 | /** |
| 45 | * This class uses an xterm/ANSI X3.64/ECMA-48 type terminal to provide a |
| 46 | * screen, keyboard, and mouse to TApplication. |
| 47 | */ |
| 48 | public class ECMA48Backend extends Backend { |
| 49 | |
| 50 | /** |
| 51 | * Input events are processed by this Terminal. |
| 52 | */ |
| 53 | private ECMA48Terminal terminal; |
| 54 | |
| 55 | /** |
| 56 | * Public constructor. |
| 57 | * |
| 58 | * @param input an InputStream connected to the remote user, or null for |
| 59 | * System.in. If System.in is used, then on non-Windows systems it will |
| 60 | * be put in raw mode; shutdown() will (blindly!) put System.in in cooked |
| 61 | * mode. input is always converted to a Reader with UTF-8 encoding. |
| 62 | * @param output an OutputStream connected to the remote user, or null |
| 63 | * for System.out. output is always converted to a Writer with UTF-8 |
| 64 | * encoding. |
| 65 | */ |
| 66 | public ECMA48Backend(InputStream input, OutputStream output) throws UnsupportedEncodingException { |
| 67 | |
| 68 | // Create a terminal and explicitly set stdin into raw mode |
| 69 | terminal = new ECMA48Terminal(input, output); |
| 70 | |
| 71 | // Create a screen |
| 72 | screen = new ECMA48Screen(terminal); |
| 73 | |
| 74 | // Clear the screen |
| 75 | terminal.getOutput().write(terminal.clearAll()); |
| 76 | terminal.flush(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sync the logical screen to the physical device. |
| 81 | */ |
| 82 | @Override |
| 83 | public void flushScreen() { |
| 84 | screen.flushPhysical(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get keyboard, mouse, and screen resize events. |
| 89 | * |
| 90 | * @param timeout maximum amount of time (in millis) to wait for an |
| 91 | * event. 0 means to return immediately, i.e. perform a poll. |
| 92 | * @return events received, or an empty list if the timeout was reached |
| 93 | * first |
| 94 | */ |
| 95 | @Override |
| 96 | public List<TInputEvent> getEvents(int timeout) { |
| 97 | return terminal.getEvents(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Subclasses must provide an implementation that closes sockets, |
| 102 | * restores console, etc. |
| 103 | */ |
| 104 | @Override |
| 105 | public void shutdown() { |
| 106 | terminal.shutdown(); |
| 107 | } |
| 108 | |
| 109 | } |