Commit | Line | Data |
---|---|---|
c8496dac KL |
1 | /** |
2 | * Jexer - Java Text User Interface | |
3 | * | |
c8496dac KL |
4 | * License: LGPLv3 or later |
5 | * | |
7b5261bc KL |
6 | * This module is licensed under the GNU Lesser General Public License |
7 | * Version 3. Please see the file "COPYING" in this directory for more | |
8 | * information about the GNU Lesser General Public License Version 3. | |
c8496dac KL |
9 | * |
10 | * Copyright (C) 2015 Kevin Lamonte | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public License | |
14 | * as published by the Free Software Foundation; either version 3 of | |
15 | * the License, or (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this program; if not, see | |
24 | * http://www.gnu.org/licenses/, or write to the Free Software | |
25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
26 | * 02110-1301 USA | |
7b5261bc KL |
27 | * |
28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
29 | * @version 1 | |
c8496dac KL |
30 | */ |
31 | package jexer.session; | |
32 | ||
33 | import java.io.BufferedReader; | |
34 | import java.io.InputStreamReader; | |
35 | import java.io.IOException; | |
36 | import java.util.Date; | |
37 | import java.util.StringTokenizer; | |
38 | ||
39 | /** | |
40 | * TTYSessionInfo queries environment variables and the tty window size for | |
41 | * the session information. The username is taken from | |
42 | * getpwuid(geteuid()).pw_name, language is taken from LANG, and text window | |
43 | * size from ioctl(TIOCGWINSIZ). | |
44 | */ | |
7b5261bc | 45 | public final class TTYSessionInfo implements SessionInfo { |
c8496dac KL |
46 | |
47 | /** | |
7b5261bc | 48 | * User name. |
c8496dac KL |
49 | */ |
50 | private String username = ""; | |
51 | ||
52 | /** | |
7b5261bc | 53 | * Language. |
c8496dac KL |
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 | /** | |
7b5261bc | 68 | * Time at which the window size was refreshed. |
c8496dac KL |
69 | */ |
70 | private Date lastQueryWindowTime; | |
7b5261bc | 71 | |
c8496dac | 72 | /** |
7b5261bc | 73 | * Username getter. |
c8496dac KL |
74 | * |
75 | * @return the username | |
76 | */ | |
77 | public String getUsername() { | |
7b5261bc | 78 | return this.username; |
c8496dac KL |
79 | } |
80 | ||
81 | /** | |
7b5261bc | 82 | * Username setter. |
c8496dac KL |
83 | * |
84 | * @param username the value | |
85 | */ | |
7b5261bc KL |
86 | public void setUsername(final String username) { |
87 | this.username = username; | |
c8496dac KL |
88 | } |
89 | ||
90 | /** | |
7b5261bc | 91 | * Language getter. |
c8496dac KL |
92 | * |
93 | * @return the language | |
94 | */ | |
95 | public String getLanguage() { | |
7b5261bc | 96 | return this.language; |
c8496dac KL |
97 | } |
98 | ||
99 | /** | |
7b5261bc | 100 | * Language setter. |
c8496dac KL |
101 | * |
102 | * @param language the value | |
103 | */ | |
7b5261bc KL |
104 | public void setLanguage(final String language) { |
105 | this.language = language; | |
c8496dac KL |
106 | } |
107 | ||
108 | /** | |
109 | * Call 'stty size' to obtain the tty window size. windowWidth and | |
110 | * windowHeight are set automatically. | |
111 | */ | |
112 | private void sttyWindowSize() { | |
7b5261bc KL |
113 | String [] cmd = { |
114 | "/bin/sh", "-c", "stty size < /dev/tty" | |
115 | }; | |
116 | try { | |
117 | Process process = Runtime.getRuntime().exec(cmd); | |
118 | BufferedReader in = new BufferedReader( | |
119 | new InputStreamReader(process.getInputStream(), "UTF-8")); | |
120 | String line = in.readLine(); | |
121 | if ((line != null) && (line.length() > 0)) { | |
122 | StringTokenizer tokenizer = new StringTokenizer(line); | |
36338168 KL |
123 | int rc = Integer.parseInt(tokenizer.nextToken()); |
124 | if (rc > 0) { | |
125 | windowHeight = rc; | |
126 | } | |
127 | rc = Integer.parseInt(tokenizer.nextToken()); | |
128 | if (rc > 0) { | |
129 | windowWidth = rc; | |
130 | } | |
7b5261bc KL |
131 | } |
132 | while (true) { | |
133 | BufferedReader err = new BufferedReader( | |
134 | new InputStreamReader(process.getErrorStream(), | |
135 | "UTF-8")); | |
136 | line = err.readLine(); | |
137 | if ((line != null) && (line.length() > 0)) { | |
138 | System.err.println("Error output from stty: " + line); | |
139 | } | |
140 | try { | |
141 | process.waitFor(); | |
142 | break; | |
143 | } catch (InterruptedException e) { | |
144 | e.printStackTrace(); | |
145 | } | |
146 | } | |
147 | int rc = process.exitValue(); | |
148 | if (rc != 0) { | |
149 | System.err.println("stty returned error code: " + rc); | |
150 | } | |
151 | } catch (IOException e) { | |
152 | e.printStackTrace(); | |
153 | } | |
c8496dac | 154 | } |
7b5261bc | 155 | |
c8496dac | 156 | /** |
7b5261bc | 157 | * Text window width getter. |
c8496dac KL |
158 | * |
159 | * @return the window width | |
160 | */ | |
161 | public int getWindowWidth() { | |
7b5261bc KL |
162 | if (System.getProperty("os.name").startsWith("Windows")) { |
163 | // Always use 80x25 for Windows (same as DOS) | |
164 | return 80; | |
165 | } | |
166 | return windowWidth; | |
c8496dac KL |
167 | } |
168 | ||
169 | /** | |
7b5261bc | 170 | * Text window height getter. |
c8496dac KL |
171 | * |
172 | * @return the window height | |
173 | */ | |
174 | public int getWindowHeight() { | |
7b5261bc KL |
175 | if (System.getProperty("os.name").startsWith("Windows")) { |
176 | // Always use 80x25 for Windows (same as DOS) | |
177 | return 25; | |
178 | } | |
179 | return windowHeight; | |
c8496dac KL |
180 | } |
181 | ||
182 | /** | |
7b5261bc | 183 | * Re-query the text window size. |
c8496dac KL |
184 | */ |
185 | public void queryWindowSize() { | |
7b5261bc KL |
186 | if (lastQueryWindowTime == null) { |
187 | lastQueryWindowTime = new Date(); | |
188 | } else { | |
189 | Date now = new Date(); | |
190 | if (now.getTime() - lastQueryWindowTime.getTime() < 3000) { | |
191 | // Don't re-spawn stty, it's been too soon. | |
192 | return; | |
193 | } | |
194 | } | |
195 | if (System.getProperty("os.name").startsWith("Linux") | |
196 | || System.getProperty("os.name").startsWith("Mac OS X") | |
197 | || System.getProperty("os.name").startsWith("SunOS") | |
198 | || System.getProperty("os.name").startsWith("FreeBSD") | |
199 | ) { | |
200 | // Use stty to get the window size | |
201 | sttyWindowSize(); | |
202 | } | |
c8496dac KL |
203 | } |
204 | ||
205 | /** | |
7b5261bc | 206 | * Public constructor. |
c8496dac KL |
207 | */ |
208 | public TTYSessionInfo() { | |
7b5261bc KL |
209 | // Populate lang and user from the environment |
210 | username = System.getProperty("user.name"); | |
211 | language = System.getProperty("user.language"); | |
212 | queryWindowSize(); | |
c8496dac KL |
213 | } |
214 | } |