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; | |
34 | import java.util.Date; | |
35 | import java.util.StringTokenizer; | |
36 | ||
37 | /** | |
38 | * TTYSessionInfo queries environment variables and the tty window size for | |
a4406f4e KL |
39 | * the session information. The username is taken from user.name, language |
40 | * is taken from user.language, and text window size from 'stty size'. | |
c8496dac | 41 | */ |
7b5261bc | 42 | public final class TTYSessionInfo implements SessionInfo { |
c8496dac KL |
43 | |
44 | /** | |
7b5261bc | 45 | * User name. |
c8496dac KL |
46 | */ |
47 | private String username = ""; | |
48 | ||
49 | /** | |
7b5261bc | 50 | * Language. |
c8496dac KL |
51 | */ |
52 | private String language = ""; | |
53 | ||
54 | /** | |
55 | * Text window width. Default is 80x24 (same as VT100-ish terminals). | |
56 | */ | |
57 | private int windowWidth = 80; | |
58 | ||
59 | /** | |
60 | * Text window height. Default is 80x24 (same as VT100-ish terminals). | |
61 | */ | |
62 | private int windowHeight = 24; | |
63 | ||
64 | /** | |
7b5261bc | 65 | * Time at which the window size was refreshed. |
c8496dac KL |
66 | */ |
67 | private Date lastQueryWindowTime; | |
7b5261bc | 68 | |
c8496dac | 69 | /** |
7b5261bc | 70 | * Username getter. |
c8496dac KL |
71 | * |
72 | * @return the username | |
73 | */ | |
74 | public String getUsername() { | |
7b5261bc | 75 | return this.username; |
c8496dac KL |
76 | } |
77 | ||
78 | /** | |
7b5261bc | 79 | * Username setter. |
c8496dac KL |
80 | * |
81 | * @param username the value | |
82 | */ | |
7b5261bc KL |
83 | public void setUsername(final String username) { |
84 | this.username = username; | |
c8496dac KL |
85 | } |
86 | ||
87 | /** | |
7b5261bc | 88 | * Language getter. |
c8496dac KL |
89 | * |
90 | * @return the language | |
91 | */ | |
92 | public String getLanguage() { | |
7b5261bc | 93 | return this.language; |
c8496dac KL |
94 | } |
95 | ||
96 | /** | |
7b5261bc | 97 | * Language setter. |
c8496dac KL |
98 | * |
99 | * @param language the value | |
100 | */ | |
7b5261bc KL |
101 | public void setLanguage(final String language) { |
102 | this.language = language; | |
c8496dac KL |
103 | } |
104 | ||
105 | /** | |
106 | * Call 'stty size' to obtain the tty window size. windowWidth and | |
107 | * windowHeight are set automatically. | |
108 | */ | |
109 | private void sttyWindowSize() { | |
7b5261bc KL |
110 | String [] cmd = { |
111 | "/bin/sh", "-c", "stty size < /dev/tty" | |
112 | }; | |
113 | try { | |
114 | Process process = Runtime.getRuntime().exec(cmd); | |
115 | BufferedReader in = new BufferedReader( | |
116 | new InputStreamReader(process.getInputStream(), "UTF-8")); | |
117 | String line = in.readLine(); | |
118 | if ((line != null) && (line.length() > 0)) { | |
119 | StringTokenizer tokenizer = new StringTokenizer(line); | |
36338168 KL |
120 | int rc = Integer.parseInt(tokenizer.nextToken()); |
121 | if (rc > 0) { | |
122 | windowHeight = rc; | |
123 | } | |
124 | rc = Integer.parseInt(tokenizer.nextToken()); | |
125 | if (rc > 0) { | |
126 | windowWidth = rc; | |
127 | } | |
7b5261bc KL |
128 | } |
129 | while (true) { | |
130 | BufferedReader err = new BufferedReader( | |
131 | new InputStreamReader(process.getErrorStream(), | |
132 | "UTF-8")); | |
133 | line = err.readLine(); | |
134 | if ((line != null) && (line.length() > 0)) { | |
135 | System.err.println("Error output from stty: " + line); | |
136 | } | |
137 | try { | |
138 | process.waitFor(); | |
139 | break; | |
140 | } catch (InterruptedException e) { | |
141 | e.printStackTrace(); | |
142 | } | |
143 | } | |
144 | int rc = process.exitValue(); | |
145 | if (rc != 0) { | |
146 | System.err.println("stty returned error code: " + rc); | |
147 | } | |
148 | } catch (IOException e) { | |
149 | e.printStackTrace(); | |
150 | } | |
c8496dac | 151 | } |
7b5261bc | 152 | |
c8496dac | 153 | /** |
7b5261bc | 154 | * Text window width getter. |
c8496dac KL |
155 | * |
156 | * @return the window width | |
157 | */ | |
158 | public int getWindowWidth() { | |
7b5261bc KL |
159 | if (System.getProperty("os.name").startsWith("Windows")) { |
160 | // Always use 80x25 for Windows (same as DOS) | |
161 | return 80; | |
162 | } | |
163 | return windowWidth; | |
c8496dac KL |
164 | } |
165 | ||
166 | /** | |
7b5261bc | 167 | * Text window height getter. |
c8496dac KL |
168 | * |
169 | * @return the window height | |
170 | */ | |
171 | public int getWindowHeight() { | |
7b5261bc KL |
172 | if (System.getProperty("os.name").startsWith("Windows")) { |
173 | // Always use 80x25 for Windows (same as DOS) | |
174 | return 25; | |
175 | } | |
176 | return windowHeight; | |
c8496dac KL |
177 | } |
178 | ||
179 | /** | |
7b5261bc | 180 | * Re-query the text window size. |
c8496dac KL |
181 | */ |
182 | public void queryWindowSize() { | |
7b5261bc KL |
183 | if (lastQueryWindowTime == null) { |
184 | lastQueryWindowTime = new Date(); | |
185 | } else { | |
186 | Date now = new Date(); | |
187 | if (now.getTime() - lastQueryWindowTime.getTime() < 3000) { | |
188 | // Don't re-spawn stty, it's been too soon. | |
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 | } |