| 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.backend; |
| 32 | |
| 33 | import java.util.List; |
| 34 | |
| 35 | import jexer.event.TInputEvent; |
| 36 | import jexer.io.AWTScreen; |
| 37 | import jexer.io.AWTTerminal; |
| 38 | |
| 39 | /** |
| 40 | * This class uses standard AWT calls to handle screen, keyboard, and mouse |
| 41 | * I/O. |
| 42 | */ |
| 43 | public final class AWTBackend extends Backend { |
| 44 | |
| 45 | /** |
| 46 | * Input events are processed by this Terminal. |
| 47 | */ |
| 48 | private AWTTerminal terminal; |
| 49 | |
| 50 | /** |
| 51 | * Public constructor. |
| 52 | */ |
| 53 | public AWTBackend() { |
| 54 | // Create a screen |
| 55 | AWTScreen screen = new AWTScreen(); |
| 56 | this.screen = screen; |
| 57 | // Create the listeners |
| 58 | terminal = new AWTTerminal(screen); |
| 59 | // Hang onto the session info |
| 60 | this.sessionInfo = terminal.getSessionInfo(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Sync the logical screen to the physical device. |
| 65 | */ |
| 66 | @Override |
| 67 | public void flushScreen() { |
| 68 | screen.flushPhysical(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get keyboard, mouse, and screen resize events. |
| 73 | * |
| 74 | * @param queue list to append new events to |
| 75 | * @param timeout maximum amount of time (in millis) to wait for an |
| 76 | * event. 0 means to return immediately, i.e. perform a poll. |
| 77 | */ |
| 78 | @Override |
| 79 | public void getEvents(final List<TInputEvent> queue, final int timeout) { |
| 80 | if (timeout > 0) { |
| 81 | // Try to sleep, let the terminal's input thread wake me up if |
| 82 | // something came in. |
| 83 | synchronized (terminal) { |
| 84 | try { |
| 85 | terminal.wait(timeout); |
| 86 | if (terminal.hasEvents()) { |
| 87 | // System.err.println("getEvents()"); |
| 88 | terminal.getEvents(queue); |
| 89 | } else { |
| 90 | // If I got here, then I timed out. Call |
| 91 | // terminal.getIdleEvents() to pick up stragglers |
| 92 | // like bare resize. |
| 93 | // System.err.println("getIdleEvents()"); |
| 94 | terminal.getIdleEvents(queue); |
| 95 | } |
| 96 | } catch (InterruptedException e) { |
| 97 | // Spurious interrupt, pretend it was like a timeout. |
| 98 | // System.err.println("[interrupt] getEvents()"); |
| 99 | terminal.getIdleEvents(queue); |
| 100 | } |
| 101 | } |
| 102 | } else { |
| 103 | // Asking for a poll, go get it. |
| 104 | // System.err.println("[polled] getEvents()"); |
| 105 | terminal.getEvents(queue); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Close the I/O, restore the console, etc. |
| 111 | */ |
| 112 | @Override |
| 113 | public void shutdown() { |
| 114 | terminal.shutdown(); |
| 115 | } |
| 116 | |
| 117 | } |