c9ddc13f0772eb116dc50327356a424460d84e0b
[fanfix.git] / src / jexer / io / AWTTerminal.java
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.io;
32
33 import java.util.List;
34 import java.util.LinkedList;
35
36 import jexer.TKeypress;
37 import jexer.bits.Color;
38 import jexer.event.TInputEvent;
39 import jexer.event.TKeypressEvent;
40 import jexer.event.TMouseEvent;
41 import jexer.event.TResizeEvent;
42 import jexer.session.SessionInfo;
43 import jexer.session.TSessionInfo;
44 import static jexer.TKeypress.*;
45
46 /**
47 * This class reads keystrokes and mouse events from an AWT Frame.
48 */
49 public final class AWTTerminal {
50
51 /**
52 * The session information.
53 */
54 private SessionInfo sessionInfo;
55
56 /**
57 * Getter for sessionInfo.
58 *
59 * @return the SessionInfo
60 */
61 public SessionInfo getSessionInfo() {
62 return sessionInfo;
63 }
64
65 /**
66 * The event queue, filled up by a thread reading on input.
67 */
68 private List<TInputEvent> eventQueue;
69
70 /**
71 * If true, we want the reader thread to exit gracefully.
72 */
73 private boolean stopReaderThread;
74
75 /**
76 * The reader thread.
77 */
78 private Thread readerThread;
79
80 /**
81 * true if mouse1 was down. Used to report mouse1 on the release event.
82 */
83 private boolean mouse1;
84
85 /**
86 * true if mouse2 was down. Used to report mouse2 on the release event.
87 */
88 private boolean mouse2;
89
90 /**
91 * true if mouse3 was down. Used to report mouse3 on the release event.
92 */
93 private boolean mouse3;
94
95 /**
96 * Check if there are events in the queue.
97 *
98 * @return if true, getEvents() has something to return to the backend
99 */
100 public boolean hasEvents() {
101 synchronized (eventQueue) {
102 return (eventQueue.size() > 0);
103 }
104 }
105
106 /**
107 * Constructor sets up state for getEvent().
108 *
109 * @param screen the top-level AWT frame
110 */
111 public AWTTerminal(final AWTScreen screen) {
112 mouse1 = false;
113 mouse2 = false;
114 mouse3 = false;
115 stopReaderThread = false;
116 sessionInfo = new TSessionInfo();
117 eventQueue = new LinkedList<TInputEvent>();
118 }
119
120 /**
121 * Restore terminal to normal state.
122 */
123 public void shutdown() {
124 // System.err.println("=== shutdown() ==="); System.err.flush();
125 }
126
127 /**
128 * Return any events in the IO queue.
129 *
130 * @param queue list to append new events to
131 */
132 public void getEvents(final List<TInputEvent> queue) {
133 synchronized (eventQueue) {
134 if (eventQueue.size() > 0) {
135 synchronized (queue) {
136 queue.addAll(eventQueue);
137 }
138 eventQueue.clear();
139 }
140 }
141 }
142
143 /**
144 * Return any events in the IO queue due to timeout.
145 *
146 * @param queue list to append new events to
147 */
148 public void getIdleEvents(final List<TInputEvent> queue) {
149
150 // Insert any polling action here...
151
152 // Return any events that showed up
153 synchronized (eventQueue) {
154 if (eventQueue.size() > 0) {
155 synchronized (queue) {
156 queue.addAll(eventQueue);
157 }
158 eventQueue.clear();
159 }
160 }
161 }
162
163 }