| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * License: LGPLv3 or later |
| 5 | * |
| 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. |
| 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 |
| 27 | * |
| 28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 29 | * @version 1 |
| 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 | */ |
| 45 | public final class TTYSessionInfo implements SessionInfo { |
| 46 | |
| 47 | /** |
| 48 | * User name. |
| 49 | */ |
| 50 | private String username = ""; |
| 51 | |
| 52 | /** |
| 53 | * Language. |
| 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 | /** |
| 68 | * Time at which the window size was refreshed. |
| 69 | */ |
| 70 | private Date lastQueryWindowTime; |
| 71 | |
| 72 | /** |
| 73 | * Username getter. |
| 74 | * |
| 75 | * @return the username |
| 76 | */ |
| 77 | public String getUsername() { |
| 78 | return this.username; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Username setter. |
| 83 | * |
| 84 | * @param username the value |
| 85 | */ |
| 86 | public void setUsername(final String username) { |
| 87 | this.username = username; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Language getter. |
| 92 | * |
| 93 | * @return the language |
| 94 | */ |
| 95 | public String getLanguage() { |
| 96 | return this.language; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Language setter. |
| 101 | * |
| 102 | * @param language the value |
| 103 | */ |
| 104 | public void setLanguage(final String language) { |
| 105 | this.language = language; |
| 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() { |
| 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); |
| 123 | windowHeight = Integer.parseInt(tokenizer.nextToken()); |
| 124 | windowWidth = Integer.parseInt(tokenizer.nextToken()); |
| 125 | } |
| 126 | while (true) { |
| 127 | BufferedReader err = new BufferedReader( |
| 128 | new InputStreamReader(process.getErrorStream(), |
| 129 | "UTF-8")); |
| 130 | line = err.readLine(); |
| 131 | if ((line != null) && (line.length() > 0)) { |
| 132 | System.err.println("Error output from stty: " + line); |
| 133 | } |
| 134 | try { |
| 135 | process.waitFor(); |
| 136 | break; |
| 137 | } catch (InterruptedException e) { |
| 138 | e.printStackTrace(); |
| 139 | } |
| 140 | } |
| 141 | int rc = process.exitValue(); |
| 142 | if (rc != 0) { |
| 143 | System.err.println("stty returned error code: " + rc); |
| 144 | } |
| 145 | } catch (IOException e) { |
| 146 | e.printStackTrace(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Text window width getter. |
| 152 | * |
| 153 | * @return the window width |
| 154 | */ |
| 155 | public int getWindowWidth() { |
| 156 | if (System.getProperty("os.name").startsWith("Windows")) { |
| 157 | // Always use 80x25 for Windows (same as DOS) |
| 158 | return 80; |
| 159 | } |
| 160 | return windowWidth; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Text window height getter. |
| 165 | * |
| 166 | * @return the window height |
| 167 | */ |
| 168 | public int getWindowHeight() { |
| 169 | if (System.getProperty("os.name").startsWith("Windows")) { |
| 170 | // Always use 80x25 for Windows (same as DOS) |
| 171 | return 25; |
| 172 | } |
| 173 | return windowHeight; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Re-query the text window size. |
| 178 | */ |
| 179 | public void queryWindowSize() { |
| 180 | if (lastQueryWindowTime == null) { |
| 181 | lastQueryWindowTime = new Date(); |
| 182 | } else { |
| 183 | Date now = new Date(); |
| 184 | if (now.getTime() - lastQueryWindowTime.getTime() < 3000) { |
| 185 | // Don't re-spawn stty, it's been too soon. |
| 186 | return; |
| 187 | } |
| 188 | } |
| 189 | if (System.getProperty("os.name").startsWith("Linux") |
| 190 | || System.getProperty("os.name").startsWith("Mac OS X") |
| 191 | || System.getProperty("os.name").startsWith("SunOS") |
| 192 | || System.getProperty("os.name").startsWith("FreeBSD") |
| 193 | ) { |
| 194 | // Use stty to get the window size |
| 195 | sttyWindowSize(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Public constructor. |
| 201 | */ |
| 202 | public TTYSessionInfo() { |
| 203 | // Populate lang and user from the environment |
| 204 | username = System.getProperty("user.name"); |
| 205 | language = System.getProperty("user.language"); |
| 206 | queryWindowSize(); |
| 207 | } |
| 208 | } |