Commit | Line | Data |
---|---|---|
05dbb28d KL |
1 | /** |
2 | * Jexer - Java Text User Interface | |
3 | * | |
05dbb28d KL |
4 | * License: LGPLv3 or later |
5 | * | |
7b5261bc KL |
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. | |
05dbb28d KL |
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 | |
7b5261bc KL |
27 | * |
28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
29 | * @version 1 | |
05dbb28d KL |
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 | */ | |
2b9c27db | 46 | public final class ECMA48Backend extends Backend { |
05dbb28d KL |
47 | |
48 | /** | |
49 | * Input events are processed by this Terminal. | |
50 | */ | |
51 | private ECMA48Terminal terminal; | |
52 | ||
53 | /** | |
54 | * Public constructor. | |
55 | * | |
92554d64 KL |
56 | * @param listener the object this backend needs to wake up when new |
57 | * input comes in | |
05dbb28d KL |
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. | |
7b5261bc KL |
65 | * @throws UnsupportedEncodingException if an exception is thrown when |
66 | * creating the InputStreamReader | |
05dbb28d | 67 | */ |
92554d64 | 68 | public ECMA48Backend(final Object listener, final InputStream input, |
7b5261bc KL |
69 | final OutputStream output) throws UnsupportedEncodingException { |
70 | ||
71 | // Create a terminal and explicitly set stdin into raw mode | |
92554d64 | 72 | terminal = new ECMA48Terminal(listener, input, output); |
05dbb28d | 73 | |
7b5261bc KL |
74 | // Keep the terminal's sessionInfo so that TApplication can see it |
75 | sessionInfo = terminal.getSessionInfo(); | |
05dbb28d | 76 | |
7b5261bc KL |
77 | // Create a screen |
78 | screen = new ECMA48Screen(terminal); | |
05dbb28d | 79 | |
7b5261bc KL |
80 | // Clear the screen |
81 | terminal.getOutput().write(terminal.clearAll()); | |
82 | terminal.flush(); | |
05dbb28d KL |
83 | } |
84 | ||
85 | /** | |
86 | * Sync the logical screen to the physical device. | |
87 | */ | |
88 | @Override | |
2b9c27db | 89 | public void flushScreen() { |
7b5261bc | 90 | screen.flushPhysical(); |
05dbb28d KL |
91 | } |
92 | ||
93 | /** | |
94 | * Get keyboard, mouse, and screen resize events. | |
95 | * | |
623a1bd1 | 96 | * @param queue list to append new events to |
05dbb28d KL |
97 | */ |
98 | @Override | |
92554d64 KL |
99 | public void getEvents(final List<TInputEvent> queue) { |
100 | if (terminal.hasEvents()) { | |
7b5261bc KL |
101 | terminal.getEvents(queue); |
102 | } | |
05dbb28d KL |
103 | } |
104 | ||
105 | /** | |
7b5261bc | 106 | * Close the I/O, restore the console, etc. |
05dbb28d KL |
107 | */ |
108 | @Override | |
2b9c27db | 109 | public void shutdown() { |
7b5261bc | 110 | terminal.shutdown(); |
05dbb28d KL |
111 | } |
112 | ||
113 | } |