| 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 { |
| 119 | System.out.println("spawn stty"); |
| 120 | |
| 121 | Process process = Runtime.getRuntime().exec(cmd); |
| 122 | BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")); |
| 123 | String line = in.readLine(); |
| 124 | if ((line != null) && (line.length() > 0)) { |
| 125 | StringTokenizer tokenizer = new StringTokenizer(line); |
| 126 | windowHeight = Integer.parseInt(tokenizer.nextToken()); |
| 127 | windowWidth = Integer.parseInt(tokenizer.nextToken()); |
| 128 | } |
| 129 | while (true) { |
| 130 | BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8")); |
| 131 | line = err.readLine(); |
| 132 | if ((line != null) && (line.length() > 0)) { |
| 133 | System.err.println("Error output from stty: " + line); |
| 134 | } |
| 135 | try{ |
| 136 | process.waitFor(); |
| 137 | break; |
| 138 | } catch (InterruptedException e) { |
| 139 | e.printStackTrace(); |
| 140 | } |
| 141 | } |
| 142 | int rc = process.exitValue(); |
| 143 | if (rc != 0) { |
| 144 | System.err.println("stty returned error code: " + rc); |
| 145 | } |
| 146 | } catch (IOException e) { |
| 147 | e.printStackTrace(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Text window width getter |
| 153 | * |
| 154 | * @return the window width |
| 155 | */ |
| 156 | public int getWindowWidth() { |
| 157 | if (System.getProperty("os.name").startsWith("Windows")) { |
| 158 | // Always use 80x25 for Windows (same as DOS) |
| 159 | return 80; |
| 160 | } |
| 161 | return windowWidth; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Text window height getter |
| 166 | * |
| 167 | * @return the window height |
| 168 | */ |
| 169 | public int getWindowHeight() { |
| 170 | if (System.getProperty("os.name").startsWith("Windows")) { |
| 171 | // Always use 80x25 for Windows (same as DOS) |
| 172 | return 25; |
| 173 | } |
| 174 | return windowHeight; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Re-query the text window size |
| 179 | */ |
| 180 | public void queryWindowSize() { |
| 181 | if (lastQueryWindowTime == null) { |
| 182 | lastQueryWindowTime = new Date(); |
| 183 | } else { |
| 184 | Date now = new Date(); |
| 185 | if (now.getTime() - lastQueryWindowTime.getTime() < 3000) { |
| 186 | // Don't re-spawn stty, it's been too soon. |
| 187 | return; |
| 188 | } |
| 189 | } |
| 190 | if (System.getProperty("os.name").startsWith("Linux") || |
| 191 | System.getProperty("os.name").startsWith("Mac OS X") || |
| 192 | System.getProperty("os.name").startsWith("SunOS") || |
| 193 | System.getProperty("os.name").startsWith("FreeBSD") |
| 194 | ) { |
| 195 | // Use stty to get the window size |
| 196 | sttyWindowSize(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Public constructor |
| 202 | */ |
| 203 | public TTYSessionInfo() { |
| 204 | // Populate lang and user from the environment |
| 205 | username = System.getProperty("user.name"); |
| 206 | language = System.getProperty("user.language"); |
| 207 | queryWindowSize(); |
| 208 | } |
| 209 | } |