#18 repaint after screen resize
[fanfix.git] / src / jexer / backend / TTYSessionInfo.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2017 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 final class TTYSessionInfo implements SessionInfo {
42
43 /**
44 * User name.
45 */
46 private String username = "";
47
48 /**
49 * Language.
50 */
51 private String language = "";
52
53 /**
54 * Text window width. Default is 80x24 (same as VT100-ish terminals).
55 */
56 private int windowWidth = 80;
57
58 /**
59 * Text window height. Default is 80x24 (same as VT100-ish terminals).
60 */
61 private int windowHeight = 24;
62
63 /**
64 * Time at which the window size was refreshed.
65 */
66 private long lastQueryWindowTime;
67
68 /**
69 * Username getter.
70 *
71 * @return the username
72 */
73 public String getUsername() {
74 return this.username;
75 }
76
77 /**
78 * Username setter.
79 *
80 * @param username the value
81 */
82 public void setUsername(final String username) {
83 this.username = username;
84 }
85
86 /**
87 * Language getter.
88 *
89 * @return the language
90 */
91 public String getLanguage() {
92 return this.language;
93 }
94
95 /**
96 * Language setter.
97 *
98 * @param language the value
99 */
100 public void setLanguage(final String language) {
101 this.language = language;
102 }
103
104 /**
105 * Call 'stty size' to obtain the tty window size. windowWidth and
106 * windowHeight are set automatically.
107 */
108 private void sttyWindowSize() {
109 String [] cmd = {
110 "/bin/sh", "-c", "stty size < /dev/tty"
111 };
112 try {
113 Process process = Runtime.getRuntime().exec(cmd);
114 BufferedReader in = new BufferedReader(
115 new InputStreamReader(process.getInputStream(), "UTF-8"));
116 String line = in.readLine();
117 if ((line != null) && (line.length() > 0)) {
118 StringTokenizer tokenizer = new StringTokenizer(line);
119 int rc = Integer.parseInt(tokenizer.nextToken());
120 if (rc > 0) {
121 windowHeight = rc;
122 }
123 rc = Integer.parseInt(tokenizer.nextToken());
124 if (rc > 0) {
125 windowWidth = rc;
126 }
127 }
128 while (true) {
129 BufferedReader err = new BufferedReader(
130 new InputStreamReader(process.getErrorStream(),
131 "UTF-8"));
132 line = err.readLine();
133 if ((line != null) && (line.length() > 0)) {
134 System.err.println("Error output from stty: " + line);
135 }
136 try {
137 process.waitFor();
138 break;
139 } catch (InterruptedException e) {
140 e.printStackTrace();
141 }
142 }
143 int rc = process.exitValue();
144 if (rc != 0) {
145 System.err.println("stty returned error code: " + rc);
146 }
147 } catch (IOException e) {
148 e.printStackTrace();
149 }
150 }
151
152 /**
153 * Text window width getter.
154 *
155 * @return the window width
156 */
157 public int getWindowWidth() {
158 if (System.getProperty("os.name").startsWith("Windows")) {
159 // Always use 80x25 for Windows (same as DOS)
160 return 80;
161 }
162 return windowWidth;
163 }
164
165 /**
166 * Text window height getter.
167 *
168 * @return the window height
169 */
170 public int getWindowHeight() {
171 if (System.getProperty("os.name").startsWith("Windows")) {
172 // Always use 80x25 for Windows (same as DOS)
173 return 25;
174 }
175 return windowHeight;
176 }
177
178 /**
179 * Re-query the text window size.
180 */
181 public void queryWindowSize() {
182 if (lastQueryWindowTime == 0) {
183 lastQueryWindowTime = System.currentTimeMillis();
184 } else {
185 long nowTime = System.currentTimeMillis();
186 if (nowTime - lastQueryWindowTime < 1000) {
187 // Don't re-spawn stty if it hasn't been a full second since
188 // the last time.
189 return;
190 }
191 }
192 if (System.getProperty("os.name").startsWith("Linux")
193 || System.getProperty("os.name").startsWith("Mac OS X")
194 || System.getProperty("os.name").startsWith("SunOS")
195 || System.getProperty("os.name").startsWith("FreeBSD")
196 ) {
197 // Use stty to get the window size
198 sttyWindowSize();
199 }
200 }
201
202 /**
203 * Public constructor.
204 */
205 public TTYSessionInfo() {
206 // Populate lang and user from the environment
207 username = System.getProperty("user.name");
208 language = System.getProperty("user.language");
209 queryWindowSize();
210 }
211 }