| 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 | */ |
| 29 | package jexer.io; |
| 30 | |
| 31 | import java.io.BufferedReader; |
| 32 | import java.io.FileDescriptor; |
| 33 | import java.io.FileInputStream; |
| 34 | import java.io.InputStream; |
| 35 | import java.io.InputStreamReader; |
| 36 | import java.io.IOException; |
| 37 | import java.io.OutputStream; |
| 38 | import java.io.OutputStreamWriter; |
| 39 | import java.io.PrintWriter; |
| 40 | import java.io.Reader; |
| 41 | import java.io.UnsupportedEncodingException; |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.Date; |
| 44 | import java.util.List; |
| 45 | import java.util.LinkedList; |
| 46 | |
| 47 | import jexer.bits.Color; |
| 48 | import jexer.event.TInputEvent; |
| 49 | import jexer.event.TKeypressEvent; |
| 50 | import jexer.event.TMouseEvent; |
| 51 | import jexer.event.TResizeEvent; |
| 52 | import jexer.session.SessionInfo; |
| 53 | import jexer.session.TSessionInfo; |
| 54 | import jexer.session.TTYSessionInfo; |
| 55 | import static jexer.TKeypress.*; |
| 56 | |
| 57 | /** |
| 58 | * This class reads keystrokes and mouse events and emits output to ANSI |
| 59 | * X3.64 / ECMA-48 type terminals e.g. xterm, linux, vt100, ansi.sys, etc. |
| 60 | */ |
| 61 | public final class ECMA48Terminal implements Runnable { |
| 62 | |
| 63 | /** |
| 64 | * The session information. |
| 65 | */ |
| 66 | private SessionInfo sessionInfo; |
| 67 | |
| 68 | /** |
| 69 | * Getter for sessionInfo. |
| 70 | * |
| 71 | * @return the SessionInfo |
| 72 | */ |
| 73 | public SessionInfo getSessionInfo() { |
| 74 | return sessionInfo; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * The event queue, filled up by a thread reading on input. |
| 79 | */ |
| 80 | private List<TInputEvent> eventQueue; |
| 81 | |
| 82 | /** |
| 83 | * If true, we want the reader thread to exit gracefully. |
| 84 | */ |
| 85 | private boolean stopReaderThread; |
| 86 | |
| 87 | /** |
| 88 | * The reader thread. |
| 89 | */ |
| 90 | private Thread readerThread; |
| 91 | |
| 92 | /** |
| 93 | * Parameters being collected. E.g. if the string is \033[1;3m, then |
| 94 | * params[0] will be 1 and params[1] will be 3. |
| 95 | */ |
| 96 | private ArrayList<String> params; |
| 97 | |
| 98 | /** |
| 99 | * States in the input parser. |
| 100 | */ |
| 101 | private enum ParseState { |
| 102 | GROUND, |
| 103 | ESCAPE, |
| 104 | ESCAPE_INTERMEDIATE, |
| 105 | CSI_ENTRY, |
| 106 | CSI_PARAM, |
| 107 | MOUSE, |
| 108 | MOUSE_SGR, |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Current parsing state. |
| 113 | */ |
| 114 | private ParseState state; |
| 115 | |
| 116 | /** |
| 117 | * The time we entered ESCAPE. If we get a bare escape without a code |
| 118 | * following it, this is used to return that bare escape. |
| 119 | */ |
| 120 | private long escapeTime; |
| 121 | |
| 122 | /** |
| 123 | * The time we last checked the window size. We try not to spawn stty |
| 124 | * more than once per second. |
| 125 | */ |
| 126 | private long windowSizeTime; |
| 127 | |
| 128 | /** |
| 129 | * true if mouse1 was down. Used to report mouse1 on the release event. |
| 130 | */ |
| 131 | private boolean mouse1; |
| 132 | |
| 133 | /** |
| 134 | * true if mouse2 was down. Used to report mouse2 on the release event. |
| 135 | */ |
| 136 | private boolean mouse2; |
| 137 | |
| 138 | /** |
| 139 | * true if mouse3 was down. Used to report mouse3 on the release event. |
| 140 | */ |
| 141 | private boolean mouse3; |
| 142 | |
| 143 | /** |
| 144 | * Cache the cursor visibility value so we only emit the sequence when we |
| 145 | * need to. |
| 146 | */ |
| 147 | private boolean cursorOn = true; |
| 148 | |
| 149 | /** |
| 150 | * Cache the last window size to figure out if a TResizeEvent needs to be |
| 151 | * generated. |
| 152 | */ |
| 153 | private TResizeEvent windowResize = null; |
| 154 | |
| 155 | /** |
| 156 | * If true, then we changed System.in and need to change it back. |
| 157 | */ |
| 158 | private boolean setRawMode; |
| 159 | |
| 160 | /** |
| 161 | * The terminal's input. If an InputStream is not specified in the |
| 162 | * constructor, then this InputStreamReader will be bound to System.in |
| 163 | * with UTF-8 encoding. |
| 164 | */ |
| 165 | private Reader input; |
| 166 | |
| 167 | /** |
| 168 | * The terminal's raw InputStream. If an InputStream is not specified in |
| 169 | * the constructor, then this InputReader will be bound to System.in. |
| 170 | * This is used by run() to see if bytes are available() before calling |
| 171 | * (Reader)input.read(). |
| 172 | */ |
| 173 | private InputStream inputStream; |
| 174 | |
| 175 | /** |
| 176 | * The terminal's output. If an OutputStream is not specified in the |
| 177 | * constructor, then this PrintWriter will be bound to System.out with |
| 178 | * UTF-8 encoding. |
| 179 | */ |
| 180 | private PrintWriter output; |
| 181 | |
| 182 | /** |
| 183 | * The listening object that run() wakes up on new input. |
| 184 | */ |
| 185 | private Object listener; |
| 186 | |
| 187 | /** |
| 188 | * Get the output writer. |
| 189 | * |
| 190 | * @return the Writer |
| 191 | */ |
| 192 | public PrintWriter getOutput() { |
| 193 | return output; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Check if there are events in the queue. |
| 198 | * |
| 199 | * @return if true, getEvents() has something to return to the backend |
| 200 | */ |
| 201 | public boolean hasEvents() { |
| 202 | synchronized (eventQueue) { |
| 203 | return (eventQueue.size() > 0); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Call 'stty' to set cooked mode. |
| 209 | * |
| 210 | * <p>Actually executes '/bin/sh -c stty sane cooked < /dev/tty' |
| 211 | */ |
| 212 | private void sttyCooked() { |
| 213 | doStty(false); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Call 'stty' to set raw mode. |
| 218 | * |
| 219 | * <p>Actually executes '/bin/sh -c stty -ignbrk -brkint -parmrk -istrip |
| 220 | * -inlcr -igncr -icrnl -ixon -opost -echo -echonl -icanon -isig -iexten |
| 221 | * -parenb cs8 min 1 < /dev/tty' |
| 222 | */ |
| 223 | private void sttyRaw() { |
| 224 | doStty(true); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Call 'stty' to set raw or cooked mode. |
| 229 | * |
| 230 | * @param mode if true, set raw mode, otherwise set cooked mode |
| 231 | */ |
| 232 | private void doStty(final boolean mode) { |
| 233 | String [] cmdRaw = { |
| 234 | "/bin/sh", "-c", "stty -ignbrk -brkint -parmrk -istrip -inlcr -igncr -icrnl -ixon -opost -echo -echonl -icanon -isig -iexten -parenb cs8 min 1 < /dev/tty" |
| 235 | }; |
| 236 | String [] cmdCooked = { |
| 237 | "/bin/sh", "-c", "stty sane cooked < /dev/tty" |
| 238 | }; |
| 239 | try { |
| 240 | Process process; |
| 241 | if (mode) { |
| 242 | process = Runtime.getRuntime().exec(cmdRaw); |
| 243 | } else { |
| 244 | process = Runtime.getRuntime().exec(cmdCooked); |
| 245 | } |
| 246 | BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")); |
| 247 | String line = in.readLine(); |
| 248 | if ((line != null) && (line.length() > 0)) { |
| 249 | System.err.println("WEIRD?! Normal output from stty: " + line); |
| 250 | } |
| 251 | while (true) { |
| 252 | BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8")); |
| 253 | line = err.readLine(); |
| 254 | if ((line != null) && (line.length() > 0)) { |
| 255 | System.err.println("Error output from stty: " + line); |
| 256 | } |
| 257 | try { |
| 258 | process.waitFor(); |
| 259 | break; |
| 260 | } catch (InterruptedException e) { |
| 261 | e.printStackTrace(); |
| 262 | } |
| 263 | } |
| 264 | int rc = process.exitValue(); |
| 265 | if (rc != 0) { |
| 266 | System.err.println("stty returned error code: " + rc); |
| 267 | } |
| 268 | } catch (IOException e) { |
| 269 | e.printStackTrace(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Constructor sets up state for getEvent(). |
| 275 | * |
| 276 | * @param listener the object this backend needs to wake up when new |
| 277 | * input comes in |
| 278 | * @param input an InputStream connected to the remote user, or null for |
| 279 | * System.in. If System.in is used, then on non-Windows systems it will |
| 280 | * be put in raw mode; shutdown() will (blindly!) put System.in in cooked |
| 281 | * mode. input is always converted to a Reader with UTF-8 encoding. |
| 282 | * @param output an OutputStream connected to the remote user, or null |
| 283 | * for System.out. output is always converted to a Writer with UTF-8 |
| 284 | * encoding. |
| 285 | * @throws UnsupportedEncodingException if an exception is thrown when |
| 286 | * creating the InputStreamReader |
| 287 | */ |
| 288 | public ECMA48Terminal(final Object listener, final InputStream input, |
| 289 | final OutputStream output) throws UnsupportedEncodingException { |
| 290 | |
| 291 | reset(); |
| 292 | mouse1 = false; |
| 293 | mouse2 = false; |
| 294 | mouse3 = false; |
| 295 | stopReaderThread = false; |
| 296 | this.listener = listener; |
| 297 | |
| 298 | if (input == null) { |
| 299 | // inputStream = System.in; |
| 300 | inputStream = new FileInputStream(FileDescriptor.in); |
| 301 | sttyRaw(); |
| 302 | setRawMode = true; |
| 303 | } else { |
| 304 | inputStream = input; |
| 305 | } |
| 306 | this.input = new InputStreamReader(inputStream, "UTF-8"); |
| 307 | |
| 308 | if (input instanceof SessionInfo) { |
| 309 | // This is a TelnetInputStream that exposes window size and |
| 310 | // environment variables from the telnet layer. |
| 311 | sessionInfo = (SessionInfo) input; |
| 312 | } |
| 313 | if (sessionInfo == null) { |
| 314 | if (input == null) { |
| 315 | // Reading right off the tty |
| 316 | sessionInfo = new TTYSessionInfo(); |
| 317 | } else { |
| 318 | sessionInfo = new TSessionInfo(); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (output == null) { |
| 323 | this.output = new PrintWriter(new OutputStreamWriter(System.out, |
| 324 | "UTF-8")); |
| 325 | } else { |
| 326 | this.output = new PrintWriter(new OutputStreamWriter(output, |
| 327 | "UTF-8")); |
| 328 | } |
| 329 | |
| 330 | // Enable mouse reporting and metaSendsEscape |
| 331 | this.output.printf("%s%s", mouse(true), xtermMetaSendsEscape(true)); |
| 332 | this.output.flush(); |
| 333 | |
| 334 | // Hang onto the window size |
| 335 | windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN, |
| 336 | sessionInfo.getWindowWidth(), sessionInfo.getWindowHeight()); |
| 337 | |
| 338 | // Spin up the input reader |
| 339 | eventQueue = new LinkedList<TInputEvent>(); |
| 340 | readerThread = new Thread(this); |
| 341 | readerThread.start(); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Constructor sets up state for getEvent(). |
| 346 | * |
| 347 | * @param listener the object this backend needs to wake up when new |
| 348 | * input comes in |
| 349 | * @param input the InputStream underlying 'reader'. Its available() |
| 350 | * method is used to determine if reader.read() will block or not. |
| 351 | * @param reader a Reader connected to the remote user. |
| 352 | * @param writer a PrintWriter connected to the remote user. |
| 353 | * @param setRawMode if true, set System.in into raw mode with stty. |
| 354 | * This should in general not be used. It is here solely for Demo3, |
| 355 | * which uses System.in. |
| 356 | * @throws IllegalArgumentException if input, reader, or writer are null. |
| 357 | */ |
| 358 | public ECMA48Terminal(final Object listener, final InputStream input, |
| 359 | final Reader reader, final PrintWriter writer, |
| 360 | final boolean setRawMode) { |
| 361 | |
| 362 | if (input == null) { |
| 363 | throw new IllegalArgumentException("InputStream must be specified"); |
| 364 | } |
| 365 | if (reader == null) { |
| 366 | throw new IllegalArgumentException("Reader must be specified"); |
| 367 | } |
| 368 | if (writer == null) { |
| 369 | throw new IllegalArgumentException("Writer must be specified"); |
| 370 | } |
| 371 | reset(); |
| 372 | mouse1 = false; |
| 373 | mouse2 = false; |
| 374 | mouse3 = false; |
| 375 | stopReaderThread = false; |
| 376 | this.listener = listener; |
| 377 | |
| 378 | inputStream = input; |
| 379 | this.input = reader; |
| 380 | |
| 381 | if (setRawMode == true) { |
| 382 | sttyRaw(); |
| 383 | } |
| 384 | this.setRawMode = setRawMode; |
| 385 | |
| 386 | if (input instanceof SessionInfo) { |
| 387 | // This is a TelnetInputStream that exposes window size and |
| 388 | // environment variables from the telnet layer. |
| 389 | sessionInfo = (SessionInfo) input; |
| 390 | } |
| 391 | if (sessionInfo == null) { |
| 392 | if (setRawMode == true) { |
| 393 | // Reading right off the tty |
| 394 | sessionInfo = new TTYSessionInfo(); |
| 395 | } else { |
| 396 | sessionInfo = new TSessionInfo(); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | this.output = writer; |
| 401 | |
| 402 | // Enable mouse reporting and metaSendsEscape |
| 403 | this.output.printf("%s%s", mouse(true), xtermMetaSendsEscape(true)); |
| 404 | this.output.flush(); |
| 405 | |
| 406 | // Hang onto the window size |
| 407 | windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN, |
| 408 | sessionInfo.getWindowWidth(), sessionInfo.getWindowHeight()); |
| 409 | |
| 410 | // Spin up the input reader |
| 411 | eventQueue = new LinkedList<TInputEvent>(); |
| 412 | readerThread = new Thread(this); |
| 413 | readerThread.start(); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Constructor sets up state for getEvent(). |
| 418 | * |
| 419 | * @param listener the object this backend needs to wake up when new |
| 420 | * input comes in |
| 421 | * @param input the InputStream underlying 'reader'. Its available() |
| 422 | * method is used to determine if reader.read() will block or not. |
| 423 | * @param reader a Reader connected to the remote user. |
| 424 | * @param writer a PrintWriter connected to the remote user. |
| 425 | * @throws IllegalArgumentException if input, reader, or writer are null. |
| 426 | */ |
| 427 | public ECMA48Terminal(final Object listener, final InputStream input, |
| 428 | final Reader reader, final PrintWriter writer) { |
| 429 | |
| 430 | this(listener, input, reader, writer, false); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Restore terminal to normal state. |
| 435 | */ |
| 436 | public void shutdown() { |
| 437 | |
| 438 | // System.err.println("=== shutdown() ==="); System.err.flush(); |
| 439 | |
| 440 | // Tell the reader thread to stop looking at input |
| 441 | stopReaderThread = true; |
| 442 | try { |
| 443 | readerThread.join(); |
| 444 | } catch (InterruptedException e) { |
| 445 | e.printStackTrace(); |
| 446 | } |
| 447 | |
| 448 | // Disable mouse reporting and show cursor |
| 449 | output.printf("%s%s%s", mouse(false), cursor(true), normal()); |
| 450 | output.flush(); |
| 451 | |
| 452 | if (setRawMode) { |
| 453 | sttyCooked(); |
| 454 | setRawMode = false; |
| 455 | // We don't close System.in/out |
| 456 | } else { |
| 457 | // Shut down the streams, this should wake up the reader thread |
| 458 | // and make it exit. |
| 459 | try { |
| 460 | if (input != null) { |
| 461 | input.close(); |
| 462 | input = null; |
| 463 | } |
| 464 | if (output != null) { |
| 465 | output.close(); |
| 466 | output = null; |
| 467 | } |
| 468 | } catch (IOException e) { |
| 469 | e.printStackTrace(); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Flush output. |
| 476 | */ |
| 477 | public void flush() { |
| 478 | output.flush(); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Reset keyboard/mouse input parser. |
| 483 | */ |
| 484 | private void reset() { |
| 485 | state = ParseState.GROUND; |
| 486 | params = new ArrayList<String>(); |
| 487 | params.clear(); |
| 488 | params.add(""); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Produce a control character or one of the special ones (ENTER, TAB, |
| 493 | * etc.). |
| 494 | * |
| 495 | * @param ch Unicode code point |
| 496 | * @param alt if true, set alt on the TKeypress |
| 497 | * @return one TKeypress event, either a control character (e.g. isKey == |
| 498 | * false, ch == 'A', ctrl == true), or a special key (e.g. isKey == true, |
| 499 | * fnKey == ESC) |
| 500 | */ |
| 501 | private TKeypressEvent controlChar(final char ch, final boolean alt) { |
| 502 | // System.err.printf("controlChar: %02x\n", ch); |
| 503 | |
| 504 | switch (ch) { |
| 505 | case 0x0D: |
| 506 | // Carriage return --> ENTER |
| 507 | return new TKeypressEvent(kbEnter, alt, false, false); |
| 508 | case 0x0A: |
| 509 | // Linefeed --> ENTER |
| 510 | return new TKeypressEvent(kbEnter, alt, false, false); |
| 511 | case 0x1B: |
| 512 | // ESC |
| 513 | return new TKeypressEvent(kbEsc, alt, false, false); |
| 514 | case '\t': |
| 515 | // TAB |
| 516 | return new TKeypressEvent(kbTab, alt, false, false); |
| 517 | default: |
| 518 | // Make all other control characters come back as the alphabetic |
| 519 | // character with the ctrl field set. So SOH would be 'A' + |
| 520 | // ctrl. |
| 521 | return new TKeypressEvent(false, 0, (char)(ch + 0x40), |
| 522 | alt, true, false); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Produce special key from CSI Pn ; Pm ; ... ~ |
| 528 | * |
| 529 | * @return one KEYPRESS event representing a special key |
| 530 | */ |
| 531 | private TInputEvent csiFnKey() { |
| 532 | int key = 0; |
| 533 | if (params.size() > 0) { |
| 534 | key = Integer.parseInt(params.get(0)); |
| 535 | } |
| 536 | boolean alt = false; |
| 537 | boolean ctrl = false; |
| 538 | boolean shift = false; |
| 539 | if (params.size() > 1) { |
| 540 | shift = csiIsShift(params.get(1)); |
| 541 | alt = csiIsAlt(params.get(1)); |
| 542 | ctrl = csiIsCtrl(params.get(1)); |
| 543 | } |
| 544 | |
| 545 | switch (key) { |
| 546 | case 1: |
| 547 | return new TKeypressEvent(kbHome, alt, ctrl, shift); |
| 548 | case 2: |
| 549 | return new TKeypressEvent(kbIns, alt, ctrl, shift); |
| 550 | case 3: |
| 551 | return new TKeypressEvent(kbDel, alt, ctrl, shift); |
| 552 | case 4: |
| 553 | return new TKeypressEvent(kbEnd, alt, ctrl, shift); |
| 554 | case 5: |
| 555 | return new TKeypressEvent(kbPgUp, alt, ctrl, shift); |
| 556 | case 6: |
| 557 | return new TKeypressEvent(kbPgDn, alt, ctrl, shift); |
| 558 | case 15: |
| 559 | return new TKeypressEvent(kbF5, alt, ctrl, shift); |
| 560 | case 17: |
| 561 | return new TKeypressEvent(kbF6, alt, ctrl, shift); |
| 562 | case 18: |
| 563 | return new TKeypressEvent(kbF7, alt, ctrl, shift); |
| 564 | case 19: |
| 565 | return new TKeypressEvent(kbF8, alt, ctrl, shift); |
| 566 | case 20: |
| 567 | return new TKeypressEvent(kbF9, alt, ctrl, shift); |
| 568 | case 21: |
| 569 | return new TKeypressEvent(kbF10, alt, ctrl, shift); |
| 570 | case 23: |
| 571 | return new TKeypressEvent(kbF11, alt, ctrl, shift); |
| 572 | case 24: |
| 573 | return new TKeypressEvent(kbF12, alt, ctrl, shift); |
| 574 | default: |
| 575 | // Unknown |
| 576 | return null; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Produce mouse events based on "Any event tracking" and UTF-8 |
| 582 | * coordinates. See |
| 583 | * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking |
| 584 | * |
| 585 | * @return a MOUSE_MOTION, MOUSE_UP, or MOUSE_DOWN event |
| 586 | */ |
| 587 | private TInputEvent parseMouse() { |
| 588 | int buttons = params.get(0).charAt(0) - 32; |
| 589 | int x = params.get(0).charAt(1) - 32 - 1; |
| 590 | int y = params.get(0).charAt(2) - 32 - 1; |
| 591 | |
| 592 | // Clamp X and Y to the physical screen coordinates. |
| 593 | if (x >= windowResize.getWidth()) { |
| 594 | x = windowResize.getWidth() - 1; |
| 595 | } |
| 596 | if (y >= windowResize.getHeight()) { |
| 597 | y = windowResize.getHeight() - 1; |
| 598 | } |
| 599 | |
| 600 | TMouseEvent.Type eventType = TMouseEvent.Type.MOUSE_DOWN; |
| 601 | boolean eventMouse1 = false; |
| 602 | boolean eventMouse2 = false; |
| 603 | boolean eventMouse3 = false; |
| 604 | boolean eventMouseWheelUp = false; |
| 605 | boolean eventMouseWheelDown = false; |
| 606 | |
| 607 | // System.err.printf("buttons: %04x\r\n", buttons); |
| 608 | |
| 609 | switch (buttons) { |
| 610 | case 0: |
| 611 | eventMouse1 = true; |
| 612 | mouse1 = true; |
| 613 | break; |
| 614 | case 1: |
| 615 | eventMouse2 = true; |
| 616 | mouse2 = true; |
| 617 | break; |
| 618 | case 2: |
| 619 | eventMouse3 = true; |
| 620 | mouse3 = true; |
| 621 | break; |
| 622 | case 3: |
| 623 | // Release or Move |
| 624 | if (!mouse1 && !mouse2 && !mouse3) { |
| 625 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 626 | } else { |
| 627 | eventType = TMouseEvent.Type.MOUSE_UP; |
| 628 | } |
| 629 | if (mouse1) { |
| 630 | mouse1 = false; |
| 631 | eventMouse1 = true; |
| 632 | } |
| 633 | if (mouse2) { |
| 634 | mouse2 = false; |
| 635 | eventMouse2 = true; |
| 636 | } |
| 637 | if (mouse3) { |
| 638 | mouse3 = false; |
| 639 | eventMouse3 = true; |
| 640 | } |
| 641 | break; |
| 642 | |
| 643 | case 32: |
| 644 | // Dragging with mouse1 down |
| 645 | eventMouse1 = true; |
| 646 | mouse1 = true; |
| 647 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 648 | break; |
| 649 | |
| 650 | case 33: |
| 651 | // Dragging with mouse2 down |
| 652 | eventMouse2 = true; |
| 653 | mouse2 = true; |
| 654 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 655 | break; |
| 656 | |
| 657 | case 34: |
| 658 | // Dragging with mouse3 down |
| 659 | eventMouse3 = true; |
| 660 | mouse3 = true; |
| 661 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 662 | break; |
| 663 | |
| 664 | case 96: |
| 665 | // Dragging with mouse2 down after wheelUp |
| 666 | eventMouse2 = true; |
| 667 | mouse2 = true; |
| 668 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 669 | break; |
| 670 | |
| 671 | case 97: |
| 672 | // Dragging with mouse2 down after wheelDown |
| 673 | eventMouse2 = true; |
| 674 | mouse2 = true; |
| 675 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 676 | break; |
| 677 | |
| 678 | case 64: |
| 679 | eventMouseWheelUp = true; |
| 680 | break; |
| 681 | |
| 682 | case 65: |
| 683 | eventMouseWheelDown = true; |
| 684 | break; |
| 685 | |
| 686 | default: |
| 687 | // Unknown, just make it motion |
| 688 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 689 | break; |
| 690 | } |
| 691 | return new TMouseEvent(eventType, x, y, x, y, |
| 692 | eventMouse1, eventMouse2, eventMouse3, |
| 693 | eventMouseWheelUp, eventMouseWheelDown); |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Produce mouse events based on "Any event tracking" and SGR |
| 698 | * coordinates. See |
| 699 | * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking |
| 700 | * |
| 701 | * @param release if true, this was a release ('m') |
| 702 | * @return a MOUSE_MOTION, MOUSE_UP, or MOUSE_DOWN event |
| 703 | */ |
| 704 | private TInputEvent parseMouseSGR(final boolean release) { |
| 705 | // SGR extended coordinates - mode 1006 |
| 706 | if (params.size() < 3) { |
| 707 | // Invalid position, bail out. |
| 708 | return null; |
| 709 | } |
| 710 | int buttons = Integer.parseInt(params.get(0)); |
| 711 | int x = Integer.parseInt(params.get(1)) - 1; |
| 712 | int y = Integer.parseInt(params.get(2)) - 1; |
| 713 | |
| 714 | // Clamp X and Y to the physical screen coordinates. |
| 715 | if (x >= windowResize.getWidth()) { |
| 716 | x = windowResize.getWidth() - 1; |
| 717 | } |
| 718 | if (y >= windowResize.getHeight()) { |
| 719 | y = windowResize.getHeight() - 1; |
| 720 | } |
| 721 | |
| 722 | TMouseEvent.Type eventType = TMouseEvent.Type.MOUSE_DOWN; |
| 723 | boolean eventMouse1 = false; |
| 724 | boolean eventMouse2 = false; |
| 725 | boolean eventMouse3 = false; |
| 726 | boolean eventMouseWheelUp = false; |
| 727 | boolean eventMouseWheelDown = false; |
| 728 | |
| 729 | if (release) { |
| 730 | eventType = TMouseEvent.Type.MOUSE_UP; |
| 731 | } |
| 732 | |
| 733 | switch (buttons) { |
| 734 | case 0: |
| 735 | eventMouse1 = true; |
| 736 | break; |
| 737 | case 1: |
| 738 | eventMouse2 = true; |
| 739 | break; |
| 740 | case 2: |
| 741 | eventMouse3 = true; |
| 742 | break; |
| 743 | case 35: |
| 744 | // Motion only, no buttons down |
| 745 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 746 | break; |
| 747 | |
| 748 | case 32: |
| 749 | // Dragging with mouse1 down |
| 750 | eventMouse1 = true; |
| 751 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 752 | break; |
| 753 | |
| 754 | case 33: |
| 755 | // Dragging with mouse2 down |
| 756 | eventMouse2 = true; |
| 757 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 758 | break; |
| 759 | |
| 760 | case 34: |
| 761 | // Dragging with mouse3 down |
| 762 | eventMouse3 = true; |
| 763 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 764 | break; |
| 765 | |
| 766 | case 96: |
| 767 | // Dragging with mouse2 down after wheelUp |
| 768 | eventMouse2 = true; |
| 769 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 770 | break; |
| 771 | |
| 772 | case 97: |
| 773 | // Dragging with mouse2 down after wheelDown |
| 774 | eventMouse2 = true; |
| 775 | eventType = TMouseEvent.Type.MOUSE_MOTION; |
| 776 | break; |
| 777 | |
| 778 | case 64: |
| 779 | eventMouseWheelUp = true; |
| 780 | break; |
| 781 | |
| 782 | case 65: |
| 783 | eventMouseWheelDown = true; |
| 784 | break; |
| 785 | |
| 786 | default: |
| 787 | // Unknown, bail out |
| 788 | return null; |
| 789 | } |
| 790 | return new TMouseEvent(eventType, x, y, x, y, |
| 791 | eventMouse1, eventMouse2, eventMouse3, |
| 792 | eventMouseWheelUp, eventMouseWheelDown); |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Return any events in the IO queue. |
| 797 | * |
| 798 | * @param queue list to append new events to |
| 799 | */ |
| 800 | public void getEvents(final List<TInputEvent> queue) { |
| 801 | synchronized (eventQueue) { |
| 802 | if (eventQueue.size() > 0) { |
| 803 | synchronized (queue) { |
| 804 | queue.addAll(eventQueue); |
| 805 | } |
| 806 | eventQueue.clear(); |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Return any events in the IO queue due to timeout. |
| 813 | * |
| 814 | * @param queue list to append new events to |
| 815 | */ |
| 816 | private void getIdleEvents(final List<TInputEvent> queue) { |
| 817 | Date now = new Date(); |
| 818 | |
| 819 | // Check for new window size |
| 820 | long windowSizeDelay = now.getTime() - windowSizeTime; |
| 821 | if (windowSizeDelay > 1000) { |
| 822 | sessionInfo.queryWindowSize(); |
| 823 | int newWidth = sessionInfo.getWindowWidth(); |
| 824 | int newHeight = sessionInfo.getWindowHeight(); |
| 825 | if ((newWidth != windowResize.getWidth()) |
| 826 | || (newHeight != windowResize.getHeight()) |
| 827 | ) { |
| 828 | TResizeEvent event = new TResizeEvent(TResizeEvent.Type.SCREEN, |
| 829 | newWidth, newHeight); |
| 830 | windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN, |
| 831 | newWidth, newHeight); |
| 832 | queue.add(event); |
| 833 | } |
| 834 | windowSizeTime = now.getTime(); |
| 835 | } |
| 836 | |
| 837 | // ESCDELAY type timeout |
| 838 | if (state == ParseState.ESCAPE) { |
| 839 | long escDelay = now.getTime() - escapeTime; |
| 840 | if (escDelay > 100) { |
| 841 | // After 0.1 seconds, assume a true escape character |
| 842 | queue.add(controlChar((char)0x1B, false)); |
| 843 | reset(); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * Returns true if the CSI parameter for a keyboard command means that |
| 850 | * shift was down. |
| 851 | */ |
| 852 | private boolean csiIsShift(final String x) { |
| 853 | if ((x.equals("2")) |
| 854 | || (x.equals("4")) |
| 855 | || (x.equals("6")) |
| 856 | || (x.equals("8")) |
| 857 | ) { |
| 858 | return true; |
| 859 | } |
| 860 | return false; |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Returns true if the CSI parameter for a keyboard command means that |
| 865 | * alt was down. |
| 866 | */ |
| 867 | private boolean csiIsAlt(final String x) { |
| 868 | if ((x.equals("3")) |
| 869 | || (x.equals("4")) |
| 870 | || (x.equals("7")) |
| 871 | || (x.equals("8")) |
| 872 | ) { |
| 873 | return true; |
| 874 | } |
| 875 | return false; |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Returns true if the CSI parameter for a keyboard command means that |
| 880 | * ctrl was down. |
| 881 | */ |
| 882 | private boolean csiIsCtrl(final String x) { |
| 883 | if ((x.equals("5")) |
| 884 | || (x.equals("6")) |
| 885 | || (x.equals("7")) |
| 886 | || (x.equals("8")) |
| 887 | ) { |
| 888 | return true; |
| 889 | } |
| 890 | return false; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * Parses the next character of input to see if an InputEvent is |
| 895 | * fully here. |
| 896 | * |
| 897 | * @param events list to append new events to |
| 898 | * @param ch Unicode code point |
| 899 | */ |
| 900 | private void processChar(final List<TInputEvent> events, final char ch) { |
| 901 | |
| 902 | // ESCDELAY type timeout |
| 903 | Date now = new Date(); |
| 904 | if (state == ParseState.ESCAPE) { |
| 905 | long escDelay = now.getTime() - escapeTime; |
| 906 | if (escDelay > 250) { |
| 907 | // After 0.25 seconds, assume a true escape character |
| 908 | events.add(controlChar((char)0x1B, false)); |
| 909 | reset(); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | // TKeypress fields |
| 914 | boolean ctrl = false; |
| 915 | boolean alt = false; |
| 916 | boolean shift = false; |
| 917 | |
| 918 | // System.err.printf("state: %s ch %c\r\n", state, ch); |
| 919 | |
| 920 | switch (state) { |
| 921 | case GROUND: |
| 922 | |
| 923 | if (ch == 0x1B) { |
| 924 | state = ParseState.ESCAPE; |
| 925 | escapeTime = now.getTime(); |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | if (ch <= 0x1F) { |
| 930 | // Control character |
| 931 | events.add(controlChar(ch, false)); |
| 932 | reset(); |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | if (ch >= 0x20) { |
| 937 | // Normal character |
| 938 | events.add(new TKeypressEvent(false, 0, ch, |
| 939 | false, false, false)); |
| 940 | reset(); |
| 941 | return; |
| 942 | } |
| 943 | |
| 944 | break; |
| 945 | |
| 946 | case ESCAPE: |
| 947 | if (ch <= 0x1F) { |
| 948 | // ALT-Control character |
| 949 | events.add(controlChar(ch, true)); |
| 950 | reset(); |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | if (ch == 'O') { |
| 955 | // This will be one of the function keys |
| 956 | state = ParseState.ESCAPE_INTERMEDIATE; |
| 957 | return; |
| 958 | } |
| 959 | |
| 960 | // '[' goes to CSI_ENTRY |
| 961 | if (ch == '[') { |
| 962 | state = ParseState.CSI_ENTRY; |
| 963 | return; |
| 964 | } |
| 965 | |
| 966 | // Everything else is assumed to be Alt-keystroke |
| 967 | if ((ch >= 'A') && (ch <= 'Z')) { |
| 968 | shift = true; |
| 969 | } |
| 970 | alt = true; |
| 971 | events.add(new TKeypressEvent(false, 0, ch, alt, ctrl, shift)); |
| 972 | reset(); |
| 973 | return; |
| 974 | |
| 975 | case ESCAPE_INTERMEDIATE: |
| 976 | if ((ch >= 'P') && (ch <= 'S')) { |
| 977 | // Function key |
| 978 | switch (ch) { |
| 979 | case 'P': |
| 980 | events.add(new TKeypressEvent(kbF1)); |
| 981 | break; |
| 982 | case 'Q': |
| 983 | events.add(new TKeypressEvent(kbF2)); |
| 984 | break; |
| 985 | case 'R': |
| 986 | events.add(new TKeypressEvent(kbF3)); |
| 987 | break; |
| 988 | case 'S': |
| 989 | events.add(new TKeypressEvent(kbF4)); |
| 990 | break; |
| 991 | default: |
| 992 | break; |
| 993 | } |
| 994 | reset(); |
| 995 | return; |
| 996 | } |
| 997 | |
| 998 | // Unknown keystroke, ignore |
| 999 | reset(); |
| 1000 | return; |
| 1001 | |
| 1002 | case CSI_ENTRY: |
| 1003 | // Numbers - parameter values |
| 1004 | if ((ch >= '0') && (ch <= '9')) { |
| 1005 | params.set(params.size() - 1, |
| 1006 | params.get(params.size() - 1) + ch); |
| 1007 | state = ParseState.CSI_PARAM; |
| 1008 | return; |
| 1009 | } |
| 1010 | // Parameter separator |
| 1011 | if (ch == ';') { |
| 1012 | params.add(""); |
| 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | if ((ch >= 0x30) && (ch <= 0x7E)) { |
| 1017 | switch (ch) { |
| 1018 | case 'A': |
| 1019 | // Up |
| 1020 | events.add(new TKeypressEvent(kbUp, alt, ctrl, shift)); |
| 1021 | reset(); |
| 1022 | return; |
| 1023 | case 'B': |
| 1024 | // Down |
| 1025 | events.add(new TKeypressEvent(kbDown, alt, ctrl, shift)); |
| 1026 | reset(); |
| 1027 | return; |
| 1028 | case 'C': |
| 1029 | // Right |
| 1030 | events.add(new TKeypressEvent(kbRight, alt, ctrl, shift)); |
| 1031 | reset(); |
| 1032 | return; |
| 1033 | case 'D': |
| 1034 | // Left |
| 1035 | events.add(new TKeypressEvent(kbLeft, alt, ctrl, shift)); |
| 1036 | reset(); |
| 1037 | return; |
| 1038 | case 'H': |
| 1039 | // Home |
| 1040 | events.add(new TKeypressEvent(kbHome)); |
| 1041 | reset(); |
| 1042 | return; |
| 1043 | case 'F': |
| 1044 | // End |
| 1045 | events.add(new TKeypressEvent(kbEnd)); |
| 1046 | reset(); |
| 1047 | return; |
| 1048 | case 'Z': |
| 1049 | // CBT - Cursor backward X tab stops (default 1) |
| 1050 | events.add(new TKeypressEvent(kbBackTab)); |
| 1051 | reset(); |
| 1052 | return; |
| 1053 | case 'M': |
| 1054 | // Mouse position |
| 1055 | state = ParseState.MOUSE; |
| 1056 | return; |
| 1057 | case '<': |
| 1058 | // Mouse position, SGR (1006) coordinates |
| 1059 | state = ParseState.MOUSE_SGR; |
| 1060 | return; |
| 1061 | default: |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | // Unknown keystroke, ignore |
| 1067 | reset(); |
| 1068 | return; |
| 1069 | |
| 1070 | case MOUSE_SGR: |
| 1071 | // Numbers - parameter values |
| 1072 | if ((ch >= '0') && (ch <= '9')) { |
| 1073 | params.set(params.size() - 1, |
| 1074 | params.get(params.size() - 1) + ch); |
| 1075 | return; |
| 1076 | } |
| 1077 | // Parameter separator |
| 1078 | if (ch == ';') { |
| 1079 | params.add(""); |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | switch (ch) { |
| 1084 | case 'M': |
| 1085 | // Generate a mouse press event |
| 1086 | TInputEvent event = parseMouseSGR(false); |
| 1087 | if (event != null) { |
| 1088 | events.add(event); |
| 1089 | } |
| 1090 | reset(); |
| 1091 | return; |
| 1092 | case 'm': |
| 1093 | // Generate a mouse release event |
| 1094 | event = parseMouseSGR(true); |
| 1095 | if (event != null) { |
| 1096 | events.add(event); |
| 1097 | } |
| 1098 | reset(); |
| 1099 | return; |
| 1100 | default: |
| 1101 | break; |
| 1102 | } |
| 1103 | |
| 1104 | // Unknown keystroke, ignore |
| 1105 | reset(); |
| 1106 | return; |
| 1107 | |
| 1108 | case CSI_PARAM: |
| 1109 | // Numbers - parameter values |
| 1110 | if ((ch >= '0') && (ch <= '9')) { |
| 1111 | params.set(params.size() - 1, |
| 1112 | params.get(params.size() - 1) + ch); |
| 1113 | state = ParseState.CSI_PARAM; |
| 1114 | return; |
| 1115 | } |
| 1116 | // Parameter separator |
| 1117 | if (ch == ';') { |
| 1118 | params.add(""); |
| 1119 | return; |
| 1120 | } |
| 1121 | |
| 1122 | if (ch == '~') { |
| 1123 | events.add(csiFnKey()); |
| 1124 | reset(); |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | if ((ch >= 0x30) && (ch <= 0x7E)) { |
| 1129 | switch (ch) { |
| 1130 | case 'A': |
| 1131 | // Up |
| 1132 | if (params.size() > 1) { |
| 1133 | shift = csiIsShift(params.get(1)); |
| 1134 | alt = csiIsAlt(params.get(1)); |
| 1135 | ctrl = csiIsCtrl(params.get(1)); |
| 1136 | } |
| 1137 | events.add(new TKeypressEvent(kbUp, alt, ctrl, shift)); |
| 1138 | reset(); |
| 1139 | return; |
| 1140 | case 'B': |
| 1141 | // Down |
| 1142 | if (params.size() > 1) { |
| 1143 | shift = csiIsShift(params.get(1)); |
| 1144 | alt = csiIsAlt(params.get(1)); |
| 1145 | ctrl = csiIsCtrl(params.get(1)); |
| 1146 | } |
| 1147 | events.add(new TKeypressEvent(kbDown, alt, ctrl, shift)); |
| 1148 | reset(); |
| 1149 | return; |
| 1150 | case 'C': |
| 1151 | // Right |
| 1152 | if (params.size() > 1) { |
| 1153 | shift = csiIsShift(params.get(1)); |
| 1154 | alt = csiIsAlt(params.get(1)); |
| 1155 | ctrl = csiIsCtrl(params.get(1)); |
| 1156 | } |
| 1157 | events.add(new TKeypressEvent(kbRight, alt, ctrl, shift)); |
| 1158 | reset(); |
| 1159 | return; |
| 1160 | case 'D': |
| 1161 | // Left |
| 1162 | if (params.size() > 1) { |
| 1163 | shift = csiIsShift(params.get(1)); |
| 1164 | alt = csiIsAlt(params.get(1)); |
| 1165 | ctrl = csiIsCtrl(params.get(1)); |
| 1166 | } |
| 1167 | events.add(new TKeypressEvent(kbLeft, alt, ctrl, shift)); |
| 1168 | reset(); |
| 1169 | return; |
| 1170 | case 'H': |
| 1171 | // Home |
| 1172 | if (params.size() > 1) { |
| 1173 | shift = csiIsShift(params.get(1)); |
| 1174 | alt = csiIsAlt(params.get(1)); |
| 1175 | ctrl = csiIsCtrl(params.get(1)); |
| 1176 | } |
| 1177 | events.add(new TKeypressEvent(kbHome, alt, ctrl, shift)); |
| 1178 | reset(); |
| 1179 | return; |
| 1180 | case 'F': |
| 1181 | // End |
| 1182 | if (params.size() > 1) { |
| 1183 | shift = csiIsShift(params.get(1)); |
| 1184 | alt = csiIsAlt(params.get(1)); |
| 1185 | ctrl = csiIsCtrl(params.get(1)); |
| 1186 | } |
| 1187 | events.add(new TKeypressEvent(kbEnd, alt, ctrl, shift)); |
| 1188 | reset(); |
| 1189 | return; |
| 1190 | default: |
| 1191 | break; |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | // Unknown keystroke, ignore |
| 1196 | reset(); |
| 1197 | return; |
| 1198 | |
| 1199 | case MOUSE: |
| 1200 | params.set(0, params.get(params.size() - 1) + ch); |
| 1201 | if (params.get(0).length() == 3) { |
| 1202 | // We have enough to generate a mouse event |
| 1203 | events.add(parseMouse()); |
| 1204 | reset(); |
| 1205 | } |
| 1206 | return; |
| 1207 | |
| 1208 | default: |
| 1209 | break; |
| 1210 | } |
| 1211 | |
| 1212 | // This "should" be impossible to reach |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * Tell (u)xterm that we want alt- keystrokes to send escape + character |
| 1218 | * rather than set the 8th bit. Anyone who wants UTF8 should want this |
| 1219 | * enabled. |
| 1220 | * |
| 1221 | * @param on if true, enable metaSendsEscape |
| 1222 | * @return the string to emit to xterm |
| 1223 | */ |
| 1224 | private String xtermMetaSendsEscape(final boolean on) { |
| 1225 | if (on) { |
| 1226 | return "\033[?1036h\033[?1034l"; |
| 1227 | } |
| 1228 | return "\033[?1036l"; |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * Create an xterm OSC sequence to change the window title. Note package |
| 1233 | * private access. |
| 1234 | * |
| 1235 | * @param title the new title |
| 1236 | * @return the string to emit to xterm |
| 1237 | */ |
| 1238 | String setTitle(final String title) { |
| 1239 | return "\033]2;" + title + "\007"; |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Create a SGR parameter sequence for a single color change. Note |
| 1244 | * package private access. |
| 1245 | * |
| 1246 | * @param color one of the Color.WHITE, Color.BLUE, etc. constants |
| 1247 | * @param foreground if true, this is a foreground color |
| 1248 | * @return the string to emit to an ANSI / ECMA-style terminal, |
| 1249 | * e.g. "\033[42m" |
| 1250 | */ |
| 1251 | String color(final Color color, final boolean foreground) { |
| 1252 | return color(color, foreground, true); |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Create a SGR parameter sequence for a single color change. |
| 1257 | * |
| 1258 | * @param color one of the Color.WHITE, Color.BLUE, etc. constants |
| 1259 | * @param foreground if true, this is a foreground color |
| 1260 | * @param header if true, make the full header, otherwise just emit the |
| 1261 | * color parameter e.g. "42;" |
| 1262 | * @return the string to emit to an ANSI / ECMA-style terminal, |
| 1263 | * e.g. "\033[42m" |
| 1264 | */ |
| 1265 | private String color(final Color color, final boolean foreground, |
| 1266 | final boolean header) { |
| 1267 | |
| 1268 | int ecmaColor = color.getValue(); |
| 1269 | |
| 1270 | // Convert Color.* values to SGR numerics |
| 1271 | if (foreground) { |
| 1272 | ecmaColor += 30; |
| 1273 | } else { |
| 1274 | ecmaColor += 40; |
| 1275 | } |
| 1276 | |
| 1277 | if (header) { |
| 1278 | return String.format("\033[%dm", ecmaColor); |
| 1279 | } else { |
| 1280 | return String.format("%d;", ecmaColor); |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * Create a SGR parameter sequence for both foreground and background |
| 1286 | * color change. Note package private access. |
| 1287 | * |
| 1288 | * @param foreColor one of the Color.WHITE, Color.BLUE, etc. constants |
| 1289 | * @param backColor one of the Color.WHITE, Color.BLUE, etc. constants |
| 1290 | * @return the string to emit to an ANSI / ECMA-style terminal, |
| 1291 | * e.g. "\033[31;42m" |
| 1292 | */ |
| 1293 | String color(final Color foreColor, final Color backColor) { |
| 1294 | return color(foreColor, backColor, true); |
| 1295 | } |
| 1296 | |
| 1297 | /** |
| 1298 | * Create a SGR parameter sequence for both foreground and |
| 1299 | * background color change. |
| 1300 | * |
| 1301 | * @param foreColor one of the Color.WHITE, Color.BLUE, etc. constants |
| 1302 | * @param backColor one of the Color.WHITE, Color.BLUE, etc. constants |
| 1303 | * @param header if true, make the full header, otherwise just emit the |
| 1304 | * color parameter e.g. "31;42;" |
| 1305 | * @return the string to emit to an ANSI / ECMA-style terminal, |
| 1306 | * e.g. "\033[31;42m" |
| 1307 | */ |
| 1308 | private String color(final Color foreColor, final Color backColor, |
| 1309 | final boolean header) { |
| 1310 | |
| 1311 | int ecmaForeColor = foreColor.getValue(); |
| 1312 | int ecmaBackColor = backColor.getValue(); |
| 1313 | |
| 1314 | // Convert Color.* values to SGR numerics |
| 1315 | ecmaBackColor += 40; |
| 1316 | ecmaForeColor += 30; |
| 1317 | |
| 1318 | if (header) { |
| 1319 | return String.format("\033[%d;%dm", ecmaForeColor, ecmaBackColor); |
| 1320 | } else { |
| 1321 | return String.format("%d;%d;", ecmaForeColor, ecmaBackColor); |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | /** |
| 1326 | * Create a SGR parameter sequence for foreground, background, and |
| 1327 | * several attributes. This sequence first resets all attributes to |
| 1328 | * default, then sets attributes as per the parameters. Note package |
| 1329 | * private access. |
| 1330 | * |
| 1331 | * @param foreColor one of the Color.WHITE, Color.BLUE, etc. constants |
| 1332 | * @param backColor one of the Color.WHITE, Color.BLUE, etc. constants |
| 1333 | * @param bold if true, set bold |
| 1334 | * @param reverse if true, set reverse |
| 1335 | * @param blink if true, set blink |
| 1336 | * @param underline if true, set underline |
| 1337 | * @return the string to emit to an ANSI / ECMA-style terminal, |
| 1338 | * e.g. "\033[0;1;31;42m" |
| 1339 | */ |
| 1340 | String color(final Color foreColor, final Color backColor, |
| 1341 | final boolean bold, final boolean reverse, final boolean blink, |
| 1342 | final boolean underline) { |
| 1343 | |
| 1344 | int ecmaForeColor = foreColor.getValue(); |
| 1345 | int ecmaBackColor = backColor.getValue(); |
| 1346 | |
| 1347 | // Convert Color.* values to SGR numerics |
| 1348 | ecmaBackColor += 40; |
| 1349 | ecmaForeColor += 30; |
| 1350 | |
| 1351 | StringBuilder sb = new StringBuilder(); |
| 1352 | if ( bold && reverse && blink && !underline ) { |
| 1353 | sb.append("\033[0;1;7;5;"); |
| 1354 | } else if ( bold && reverse && !blink && !underline ) { |
| 1355 | sb.append("\033[0;1;7;"); |
| 1356 | } else if ( !bold && reverse && blink && !underline ) { |
| 1357 | sb.append("\033[0;7;5;"); |
| 1358 | } else if ( bold && !reverse && blink && !underline ) { |
| 1359 | sb.append("\033[0;1;5;"); |
| 1360 | } else if ( bold && !reverse && !blink && !underline ) { |
| 1361 | sb.append("\033[0;1;"); |
| 1362 | } else if ( !bold && reverse && !blink && !underline ) { |
| 1363 | sb.append("\033[0;7;"); |
| 1364 | } else if ( !bold && !reverse && blink && !underline) { |
| 1365 | sb.append("\033[0;5;"); |
| 1366 | } else if ( bold && reverse && blink && underline ) { |
| 1367 | sb.append("\033[0;1;7;5;4;"); |
| 1368 | } else if ( bold && reverse && !blink && underline ) { |
| 1369 | sb.append("\033[0;1;7;4;"); |
| 1370 | } else if ( !bold && reverse && blink && underline ) { |
| 1371 | sb.append("\033[0;7;5;4;"); |
| 1372 | } else if ( bold && !reverse && blink && underline ) { |
| 1373 | sb.append("\033[0;1;5;4;"); |
| 1374 | } else if ( bold && !reverse && !blink && underline ) { |
| 1375 | sb.append("\033[0;1;4;"); |
| 1376 | } else if ( !bold && reverse && !blink && underline ) { |
| 1377 | sb.append("\033[0;7;4;"); |
| 1378 | } else if ( !bold && !reverse && blink && underline) { |
| 1379 | sb.append("\033[0;5;4;"); |
| 1380 | } else if ( !bold && !reverse && !blink && underline) { |
| 1381 | sb.append("\033[0;4;"); |
| 1382 | } else { |
| 1383 | assert (!bold && !reverse && !blink && !underline); |
| 1384 | sb.append("\033[0;"); |
| 1385 | } |
| 1386 | sb.append(String.format("%d;%dm", ecmaForeColor, ecmaBackColor)); |
| 1387 | return sb.toString(); |
| 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * Create a SGR parameter sequence to reset to defaults. Note package |
| 1392 | * private access. |
| 1393 | * |
| 1394 | * @return the string to emit to an ANSI / ECMA-style terminal, |
| 1395 | * e.g. "\033[0m" |
| 1396 | */ |
| 1397 | String normal() { |
| 1398 | return normal(true); |
| 1399 | } |
| 1400 | |
| 1401 | /** |
| 1402 | * Create a SGR parameter sequence to reset to defaults. |
| 1403 | * |
| 1404 | * @param header if true, make the full header, otherwise just emit the |
| 1405 | * bare parameter e.g. "0;" |
| 1406 | * @return the string to emit to an ANSI / ECMA-style terminal, |
| 1407 | * e.g. "\033[0m" |
| 1408 | */ |
| 1409 | private String normal(final boolean header) { |
| 1410 | if (header) { |
| 1411 | return "\033[0;37;40m"; |
| 1412 | } |
| 1413 | return "0;37;40"; |
| 1414 | } |
| 1415 | |
| 1416 | /** |
| 1417 | * Create a SGR parameter sequence for enabling the visible cursor. Note |
| 1418 | * package private access. |
| 1419 | * |
| 1420 | * @param on if true, turn on cursor |
| 1421 | * @return the string to emit to an ANSI / ECMA-style terminal |
| 1422 | */ |
| 1423 | String cursor(final boolean on) { |
| 1424 | if (on && !cursorOn) { |
| 1425 | cursorOn = true; |
| 1426 | return "\033[?25h"; |
| 1427 | } |
| 1428 | if (!on && cursorOn) { |
| 1429 | cursorOn = false; |
| 1430 | return "\033[?25l"; |
| 1431 | } |
| 1432 | return ""; |
| 1433 | } |
| 1434 | |
| 1435 | /** |
| 1436 | * Clear the entire screen. Because some terminals use back-color-erase, |
| 1437 | * set the color to white-on-black beforehand. |
| 1438 | * |
| 1439 | * @return the string to emit to an ANSI / ECMA-style terminal |
| 1440 | */ |
| 1441 | public String clearAll() { |
| 1442 | return "\033[0;37;40m\033[2J"; |
| 1443 | } |
| 1444 | |
| 1445 | /** |
| 1446 | * Clear the line from the cursor (inclusive) to the end of the screen. |
| 1447 | * Because some terminals use back-color-erase, set the color to |
| 1448 | * white-on-black beforehand. Note package private access. |
| 1449 | * |
| 1450 | * @return the string to emit to an ANSI / ECMA-style terminal |
| 1451 | */ |
| 1452 | String clearRemainingLine() { |
| 1453 | return "\033[0;37;40m\033[K"; |
| 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * Move the cursor to (x, y). Note package private access. |
| 1458 | * |
| 1459 | * @param x column coordinate. 0 is the left-most column. |
| 1460 | * @param y row coordinate. 0 is the top-most row. |
| 1461 | * @return the string to emit to an ANSI / ECMA-style terminal |
| 1462 | */ |
| 1463 | String gotoXY(final int x, final int y) { |
| 1464 | return String.format("\033[%d;%dH", y + 1, x + 1); |
| 1465 | } |
| 1466 | |
| 1467 | /** |
| 1468 | * Tell (u)xterm that we want to receive mouse events based on "Any event |
| 1469 | * tracking", UTF-8 coordinates, and then SGR coordinates. Ideally we |
| 1470 | * will end up with SGR coordinates with UTF-8 coordinates as a fallback. |
| 1471 | * See |
| 1472 | * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking |
| 1473 | * |
| 1474 | * Note that this also sets the alternate/primary screen buffer. |
| 1475 | * |
| 1476 | * @param on If true, enable mouse report and use the alternate screen |
| 1477 | * buffer. If false disable mouse reporting and use the primary screen |
| 1478 | * buffer. |
| 1479 | * @return the string to emit to xterm |
| 1480 | */ |
| 1481 | private String mouse(final boolean on) { |
| 1482 | if (on) { |
| 1483 | return "\033[?1002;1003;1005;1006h\033[?1049h"; |
| 1484 | } |
| 1485 | return "\033[?1002;1003;1006;1005l\033[?1049l"; |
| 1486 | } |
| 1487 | |
| 1488 | /** |
| 1489 | * Read function runs on a separate thread. |
| 1490 | */ |
| 1491 | public void run() { |
| 1492 | boolean done = false; |
| 1493 | // available() will often return > 1, so we need to read in chunks to |
| 1494 | // stay caught up. |
| 1495 | char [] readBuffer = new char[128]; |
| 1496 | List<TInputEvent> events = new LinkedList<TInputEvent>(); |
| 1497 | |
| 1498 | while (!done && !stopReaderThread) { |
| 1499 | try { |
| 1500 | // We assume that if inputStream has bytes available, then |
| 1501 | // input won't block on read(). |
| 1502 | int n = inputStream.available(); |
| 1503 | if (n > 0) { |
| 1504 | if (readBuffer.length < n) { |
| 1505 | // The buffer wasn't big enough, make it huger |
| 1506 | readBuffer = new char[readBuffer.length * 2]; |
| 1507 | } |
| 1508 | |
| 1509 | int rc = input.read(readBuffer, 0, readBuffer.length); |
| 1510 | // System.err.printf("read() %d", rc); System.err.flush(); |
| 1511 | if (rc == -1) { |
| 1512 | // This is EOF |
| 1513 | done = true; |
| 1514 | } else { |
| 1515 | for (int i = 0; i < rc; i++) { |
| 1516 | int ch = readBuffer[i]; |
| 1517 | processChar(events, (char)ch); |
| 1518 | } |
| 1519 | getIdleEvents(events); |
| 1520 | if (events.size() > 0) { |
| 1521 | // Add to the queue for the backend thread to |
| 1522 | // be able to obtain. |
| 1523 | synchronized (eventQueue) { |
| 1524 | eventQueue.addAll(events); |
| 1525 | } |
| 1526 | synchronized (listener) { |
| 1527 | listener.notifyAll(); |
| 1528 | } |
| 1529 | events.clear(); |
| 1530 | } |
| 1531 | } |
| 1532 | } else { |
| 1533 | getIdleEvents(events); |
| 1534 | if (events.size() > 0) { |
| 1535 | synchronized (eventQueue) { |
| 1536 | eventQueue.addAll(events); |
| 1537 | } |
| 1538 | events.clear(); |
| 1539 | synchronized (listener) { |
| 1540 | listener.notifyAll(); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | // Wait 10 millis for more data |
| 1545 | Thread.sleep(10); |
| 1546 | } |
| 1547 | // System.err.println("end while loop"); System.err.flush(); |
| 1548 | } catch (InterruptedException e) { |
| 1549 | // SQUASH |
| 1550 | } catch (IOException e) { |
| 1551 | e.printStackTrace(); |
| 1552 | done = true; |
| 1553 | } |
| 1554 | } // while ((done == false) && (stopReaderThread == false)) |
| 1555 | // System.err.println("*** run() exiting..."); System.err.flush(); |
| 1556 | } |
| 1557 | |
| 1558 | } |