Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
c8496dac KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
c8496dac | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 Kevin Lamonte |
c8496dac | 7 | * |
e16dda65 KL |
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: | |
c8496dac | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
c8496dac | 17 | * |
e16dda65 KL |
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. | |
7b5261bc KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
c8496dac | 28 | */ |
42873e30 | 29 | package jexer.backend; |
c8496dac KL |
30 | |
31 | import java.io.BufferedReader; | |
32 | import java.io.InputStreamReader; | |
33 | import java.io.IOException; | |
c8496dac KL |
34 | import java.util.StringTokenizer; |
35 | ||
36 | /** | |
37 | * TTYSessionInfo queries environment variables and the tty window size for | |
a4406f4e KL |
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'. | |
c8496dac | 40 | */ |
7b5261bc | 41 | public final class TTYSessionInfo implements SessionInfo { |
c8496dac KL |
42 | |
43 | /** | |
7b5261bc | 44 | * User name. |
c8496dac KL |
45 | */ |
46 | private String username = ""; | |
47 | ||
48 | /** | |
7b5261bc | 49 | * Language. |
c8496dac KL |
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 | /** | |
7b5261bc | 64 | * Time at which the window size was refreshed. |
c8496dac | 65 | */ |
be72cb5c | 66 | private long lastQueryWindowTime; |
7b5261bc | 67 | |
c8496dac | 68 | /** |
7b5261bc | 69 | * Username getter. |
c8496dac KL |
70 | * |
71 | * @return the username | |
72 | */ | |
73 | public String getUsername() { | |
7b5261bc | 74 | return this.username; |
c8496dac KL |
75 | } |
76 | ||
77 | /** | |
7b5261bc | 78 | * Username setter. |
c8496dac KL |
79 | * |
80 | * @param username the value | |
81 | */ | |
7b5261bc KL |
82 | public void setUsername(final String username) { |
83 | this.username = username; | |
c8496dac KL |
84 | } |
85 | ||
86 | /** | |
7b5261bc | 87 | * Language getter. |
c8496dac KL |
88 | * |
89 | * @return the language | |
90 | */ | |
91 | public String getLanguage() { | |
7b5261bc | 92 | return this.language; |
c8496dac KL |
93 | } |
94 | ||
95 | /** | |
7b5261bc | 96 | * Language setter. |
c8496dac KL |
97 | * |
98 | * @param language the value | |
99 | */ | |
7b5261bc KL |
100 | public void setLanguage(final String language) { |
101 | this.language = language; | |
c8496dac KL |
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() { | |
7b5261bc KL |
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); | |
36338168 KL |
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 | } | |
7b5261bc KL |
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 | } | |
c8496dac | 150 | } |
7b5261bc | 151 | |
c8496dac | 152 | /** |
7b5261bc | 153 | * Text window width getter. |
c8496dac KL |
154 | * |
155 | * @return the window width | |
156 | */ | |
157 | public int getWindowWidth() { | |
7b5261bc KL |
158 | if (System.getProperty("os.name").startsWith("Windows")) { |
159 | // Always use 80x25 for Windows (same as DOS) | |
160 | return 80; | |
161 | } | |
162 | return windowWidth; | |
c8496dac KL |
163 | } |
164 | ||
165 | /** | |
7b5261bc | 166 | * Text window height getter. |
c8496dac KL |
167 | * |
168 | * @return the window height | |
169 | */ | |
170 | public int getWindowHeight() { | |
7b5261bc KL |
171 | if (System.getProperty("os.name").startsWith("Windows")) { |
172 | // Always use 80x25 for Windows (same as DOS) | |
173 | return 25; | |
174 | } | |
175 | return windowHeight; | |
c8496dac KL |
176 | } |
177 | ||
178 | /** | |
7b5261bc | 179 | * Re-query the text window size. |
c8496dac KL |
180 | */ |
181 | public void queryWindowSize() { | |
be72cb5c KL |
182 | if (lastQueryWindowTime == 0) { |
183 | lastQueryWindowTime = System.currentTimeMillis(); | |
7b5261bc | 184 | } else { |
be72cb5c | 185 | long nowTime = System.currentTimeMillis(); |
85c07c5e KL |
186 | if (nowTime - lastQueryWindowTime < 1000) { |
187 | // Don't re-spawn stty if it hasn't been a full second since | |
188 | // the last time. | |
7b5261bc KL |
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 | } | |
c8496dac KL |
200 | } |
201 | ||
202 | /** | |
7b5261bc | 203 | * Public constructor. |
c8496dac KL |
204 | */ |
205 | public TTYSessionInfo() { | |
7b5261bc KL |
206 | // Populate lang and user from the environment |
207 | username = System.getProperty("user.name"); | |
208 | language = System.getProperty("user.language"); | |
209 | queryWindowSize(); | |
c8496dac KL |
210 | } |
211 | } |