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