Commit | Line | Data |
---|---|---|
05dbb28d KL |
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 | * | |
623a1bd1 | 90 | * @param queue list to append new events to |
05dbb28d KL |
91 | * @param timeout maximum amount of time (in millis) to wait for an |
92 | * event. 0 means to return immediately, i.e. perform a poll. | |
05dbb28d KL |
93 | */ |
94 | @Override | |
623a1bd1 KL |
95 | public void getEvents(List<TInputEvent> queue, int timeout) { |
96 | if (timeout > 0) { | |
97 | // Try to sleep, let the terminal's input thread wake me up if | |
98 | // something came in. | |
99 | synchronized (terminal) { | |
100 | try { | |
101 | terminal.wait(timeout); | |
102 | if (terminal.hasEvents()) { | |
103 | // System.err.println("getEvents()"); | |
104 | terminal.getIdleEvents(queue); | |
105 | } else { | |
106 | // If I got here, then I timed out. Call | |
107 | // terminal.getIdleEvents() to pick up stragglers | |
108 | // like bare resize. | |
109 | // System.err.println("getIdleEvents()"); | |
110 | terminal.getIdleEvents(queue); | |
111 | } | |
112 | } catch (InterruptedException e) { | |
113 | // Spurious interrupt, pretend it was like a timeout. | |
114 | // System.err.println("[interrupt] getEvents()"); | |
115 | terminal.getIdleEvents(queue); | |
116 | } | |
117 | } | |
118 | } else { | |
119 | // Asking for a poll, go get it. | |
120 | System.err.println("[polled] getEvents()"); | |
121 | terminal.getEvents(queue); | |
122 | } | |
05dbb28d KL |
123 | } |
124 | ||
125 | /** | |
126 | * Subclasses must provide an implementation that closes sockets, | |
127 | * restores console, etc. | |
128 | */ | |
129 | @Override | |
130 | public void shutdown() { | |
131 | terminal.shutdown(); | |
132 | } | |
133 | ||
134 | } |