mlterm fully supports features
[fanfix.git] / src / jexer / backend / TTYSessionInfo.java
CommitLineData
daa4106c 1/*
c8496dac
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
c8496dac 5 *
a69ed767 6 * Copyright (C) 2019 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 29package jexer.backend;
c8496dac
KL
30
31import java.io.BufferedReader;
32import java.io.InputStreamReader;
33import java.io.IOException;
c8496dac
KL
34import 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 */
051e2913 41public class TTYSessionInfo implements SessionInfo {
c8496dac 42
d36057df
KL
43 // ------------------------------------------------------------------------
44 // Variables --------------------------------------------------------------
45 // ------------------------------------------------------------------------
46
c8496dac 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 69 */
be72cb5c 70 private long lastQueryWindowTime;
7b5261bc 71
d36057df
KL
72 // ------------------------------------------------------------------------
73 // Constructors -----------------------------------------------------------
74 // ------------------------------------------------------------------------
75
76 /**
77 * Public constructor.
78 */
79 public TTYSessionInfo() {
80 // Populate lang and user from the environment
81 username = System.getProperty("user.name");
82 language = System.getProperty("user.language");
83 queryWindowSize();
84 }
85
86 // ------------------------------------------------------------------------
87 // SessionInfo ------------------------------------------------------------
88 // ------------------------------------------------------------------------
89
c8496dac 90 /**
7b5261bc 91 * Username getter.
c8496dac
KL
92 *
93 * @return the username
94 */
95 public String getUsername() {
7b5261bc 96 return this.username;
c8496dac
KL
97 }
98
99 /**
7b5261bc 100 * Username setter.
c8496dac
KL
101 *
102 * @param username the value
103 */
7b5261bc
KL
104 public void setUsername(final String username) {
105 this.username = username;
c8496dac
KL
106 }
107
108 /**
7b5261bc 109 * Language getter.
c8496dac
KL
110 *
111 * @return the language
112 */
113 public String getLanguage() {
7b5261bc 114 return this.language;
c8496dac
KL
115 }
116
117 /**
7b5261bc 118 * Language setter.
c8496dac
KL
119 *
120 * @param language the value
121 */
7b5261bc
KL
122 public void setLanguage(final String language) {
123 this.language = language;
c8496dac
KL
124 }
125
c8496dac 126 /**
7b5261bc 127 * Text window width getter.
c8496dac
KL
128 *
129 * @return the window width
130 */
131 public int getWindowWidth() {
7b5261bc
KL
132 if (System.getProperty("os.name").startsWith("Windows")) {
133 // Always use 80x25 for Windows (same as DOS)
134 return 80;
135 }
136 return windowWidth;
c8496dac
KL
137 }
138
139 /**
7b5261bc 140 * Text window height getter.
c8496dac
KL
141 *
142 * @return the window height
143 */
144 public int getWindowHeight() {
7b5261bc
KL
145 if (System.getProperty("os.name").startsWith("Windows")) {
146 // Always use 80x25 for Windows (same as DOS)
147 return 25;
148 }
149 return windowHeight;
c8496dac
KL
150 }
151
152 /**
7b5261bc 153 * Re-query the text window size.
c8496dac
KL
154 */
155 public void queryWindowSize() {
be72cb5c
KL
156 if (lastQueryWindowTime == 0) {
157 lastQueryWindowTime = System.currentTimeMillis();
7b5261bc 158 } else {
be72cb5c 159 long nowTime = System.currentTimeMillis();
85c07c5e
KL
160 if (nowTime - lastQueryWindowTime < 1000) {
161 // Don't re-spawn stty if it hasn't been a full second since
162 // the last time.
7b5261bc
KL
163 return;
164 }
165 }
166 if (System.getProperty("os.name").startsWith("Linux")
167 || System.getProperty("os.name").startsWith("Mac OS X")
168 || System.getProperty("os.name").startsWith("SunOS")
169 || System.getProperty("os.name").startsWith("FreeBSD")
170 ) {
171 // Use stty to get the window size
172 sttyWindowSize();
173 }
c8496dac
KL
174 }
175
d36057df
KL
176 // ------------------------------------------------------------------------
177 // TTYSessionInfo ---------------------------------------------------------
178 // ------------------------------------------------------------------------
179
c8496dac 180 /**
d36057df
KL
181 * Call 'stty size' to obtain the tty window size. windowWidth and
182 * windowHeight are set automatically.
c8496dac 183 */
d36057df
KL
184 private void sttyWindowSize() {
185 String [] cmd = {
186 "/bin/sh", "-c", "stty size < /dev/tty"
187 };
188 try {
189 Process process = Runtime.getRuntime().exec(cmd);
190 BufferedReader in = new BufferedReader(
191 new InputStreamReader(process.getInputStream(), "UTF-8"));
192 String line = in.readLine();
193 if ((line != null) && (line.length() > 0)) {
194 StringTokenizer tokenizer = new StringTokenizer(line);
195 int rc = Integer.parseInt(tokenizer.nextToken());
196 if (rc > 0) {
197 windowHeight = rc;
198 }
199 rc = Integer.parseInt(tokenizer.nextToken());
200 if (rc > 0) {
201 windowWidth = rc;
202 }
203 }
204 while (true) {
205 BufferedReader err = new BufferedReader(
206 new InputStreamReader(process.getErrorStream(),
207 "UTF-8"));
208 line = err.readLine();
209 if ((line != null) && (line.length() > 0)) {
210 System.err.println("Error output from stty: " + line);
211 }
212 try {
213 process.waitFor();
214 break;
215 } catch (InterruptedException e) {
a69ed767 216 // SQUASH
d36057df
KL
217 }
218 }
219 int rc = process.exitValue();
220 if (rc != 0) {
221 System.err.println("stty returned error code: " + rc);
222 }
223 } catch (IOException e) {
224 e.printStackTrace();
225 }
c8496dac 226 }
d36057df 227
c8496dac 228}