Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[fanfix.git] / src / jexer / backend / TTYSessionInfo.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.backend;
30
31 import java.io.BufferedReader;
32 import java.io.InputStreamReader;
33 import java.io.IOException;
34 import java.util.StringTokenizer;
35
36 /**
37 * TTYSessionInfo queries environment variables and the tty window size for
38 * the session information. The username is taken from user.name, language
39 * is taken from user.language, and text window size from 'stty size'.
40 */
41 public class TTYSessionInfo implements SessionInfo {
42
43 // ------------------------------------------------------------------------
44 // Variables --------------------------------------------------------------
45 // ------------------------------------------------------------------------
46
47 /**
48 * User name.
49 */
50 private String username = "";
51
52 /**
53 * Language.
54 */
55 private String language = "";
56
57 /**
58 * Text window width. Default is 80x24 (same as VT100-ish terminals).
59 */
60 private int windowWidth = 80;
61
62 /**
63 * Text window height. Default is 80x24 (same as VT100-ish terminals).
64 */
65 private int windowHeight = 24;
66
67 /**
68 * Time at which the window size was refreshed.
69 */
70 private long lastQueryWindowTime;
71
72 // ------------------------------------------------------------------------
73 // Constructors -----------------------------------------------------------
74 // ------------------------------------------------------------------------
75
76 /**
77 * Public constructor.
78 */
79 public TTYSessionInfo() {
80 // Populate lang and user from the environment
81 username = System.getProperty("user.name");
82 language = System.getProperty("user.language");
83 queryWindowSize();
84 }
85
86 // ------------------------------------------------------------------------
87 // SessionInfo ------------------------------------------------------------
88 // ------------------------------------------------------------------------
89
90 /**
91 * Username getter.
92 *
93 * @return the username
94 */
95 public String getUsername() {
96 return this.username;
97 }
98
99 /**
100 * Username setter.
101 *
102 * @param username the value
103 */
104 public void setUsername(final String username) {
105 this.username = username;
106 }
107
108 /**
109 * Language getter.
110 *
111 * @return the language
112 */
113 public String getLanguage() {
114 return this.language;
115 }
116
117 /**
118 * Language setter.
119 *
120 * @param language the value
121 */
122 public void setLanguage(final String language) {
123 this.language = language;
124 }
125
126 /**
127 * Text window width getter.
128 *
129 * @return the window width
130 */
131 public int getWindowWidth() {
132 if (System.getProperty("os.name").startsWith("Windows")) {
133 // Always use 80x25 for Windows (same as DOS)
134 return 80;
135 }
136 return windowWidth;
137 }
138
139 /**
140 * Text window height getter.
141 *
142 * @return the window height
143 */
144 public int getWindowHeight() {
145 if (System.getProperty("os.name").startsWith("Windows")) {
146 // Always use 80x25 for Windows (same as DOS)
147 return 25;
148 }
149 return windowHeight;
150 }
151
152 /**
153 * Re-query the text window size.
154 */
155 public void queryWindowSize() {
156 if (lastQueryWindowTime == 0) {
157 lastQueryWindowTime = System.currentTimeMillis();
158 } else {
159 long nowTime = System.currentTimeMillis();
160 if (nowTime - lastQueryWindowTime < 1000) {
161 // Don't re-spawn stty if it hasn't been a full second since
162 // the last time.
163 return;
164 }
165 }
166 if (System.getProperty("os.name").startsWith("Linux")
167 || System.getProperty("os.name").startsWith("Mac OS X")
168 || System.getProperty("os.name").startsWith("SunOS")
169 || System.getProperty("os.name").startsWith("FreeBSD")
170 ) {
171 // Use stty to get the window size
172 sttyWindowSize();
173 }
174 }
175
176 // ------------------------------------------------------------------------
177 // TTYSessionInfo ---------------------------------------------------------
178 // ------------------------------------------------------------------------
179
180 /**
181 * Call 'stty size' to obtain the tty window size. windowWidth and
182 * windowHeight are set automatically.
183 */
184 private void sttyWindowSize() {
185 String [] cmd = {
186 "/bin/sh", "-c", "stty size < /dev/tty"
187 };
188 try {
189 Process process = Runtime.getRuntime().exec(cmd);
190 BufferedReader in = new BufferedReader(
191 new InputStreamReader(process.getInputStream(), "UTF-8"));
192 String line = in.readLine();
193 if ((line != null) && (line.length() > 0)) {
194 StringTokenizer tokenizer = new StringTokenizer(line);
195 int rc = Integer.parseInt(tokenizer.nextToken());
196 if (rc > 0) {
197 windowHeight = rc;
198 }
199 rc = Integer.parseInt(tokenizer.nextToken());
200 if (rc > 0) {
201 windowWidth = rc;
202 }
203 }
204 while (true) {
205 BufferedReader err = new BufferedReader(
206 new InputStreamReader(process.getErrorStream(),
207 "UTF-8"));
208 line = err.readLine();
209 if ((line != null) && (line.length() > 0)) {
210 System.err.println("Error output from stty: " + line);
211 }
212 try {
213 process.waitFor();
214 break;
215 } catch (InterruptedException e) {
216 // SQUASH
217 }
218 }
219 int rc = process.exitValue();
220 if (rc != 0) {
221 System.err.println("stty returned error code: " + rc);
222 }
223 } catch (IOException e) {
224 e.printStackTrace();
225 }
226 }
227
228 }