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