2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.backend
;
31 import java
.util
.LinkedList
;
32 import java
.util
.List
;
34 import jexer
.event
.TInputEvent
;
35 import jexer
.event
.TKeypressEvent
;
36 import jexer
.event
.TMouseEvent
;
37 import jexer
.event
.TResizeEvent
;
38 import jexer
.TApplication
;
42 * TWindowBackend uses a window in one TApplication to provide a backend for
43 * another TApplication.
45 * Note that TWindow has its own getScreen() and setTitle() functions.
46 * Clients in TWindowBackend's application won't be able to use it to get at
47 * the other application's screen. getOtherScreen() has been provided.
49 public class TWindowBackend
extends TWindow
implements Backend
{
51 // ------------------------------------------------------------------------
52 // Variables --------------------------------------------------------------
53 // ------------------------------------------------------------------------
56 * The listening object that run() wakes up on new input.
58 private Object listener
;
61 * The object to sync on in draw(). This is normally otherScreen, but it
62 * could also be a MultiScreen.
64 private Object drawLock
;
67 * The event queue, filled up by a thread reading on input.
69 private List
<TInputEvent
> eventQueue
;
72 * The screen this window is monitoring.
74 private Screen otherScreen
;
77 * The application associated with otherScreen.
79 private TApplication otherApplication
;
82 * The session information.
84 private SessionInfo sessionInfo
;
87 * OtherScreen provides a hook to notify TWindowBackend of screen size
90 private class OtherScreen
extends LogicalScreen
{
93 * The TWindowBackend to notify.
95 private TWindowBackend window
;
100 public OtherScreen(final TWindowBackend window
) {
101 this.window
= window
;
105 * Resize the physical screen to match the logical screen dimensions.
108 public void resizeToScreen() {
109 window
.setWidth(getWidth() + 2);
110 window
.setHeight(getHeight() + 2);
116 // ------------------------------------------------------------------------
117 // Constructors -----------------------------------------------------------
118 // ------------------------------------------------------------------------
121 * Public constructor. Window will be located at (0, 0).
123 * @param listener the object this backend needs to wake up when new
125 * @param application TApplication that manages this window
126 * @param title window title, will be centered along the top border
127 * @param width width of window
128 * @param height height of window
130 public TWindowBackend(final Object listener
,
131 final TApplication application
, final String title
,
132 final int width
, final int height
) {
134 super(application
, title
, width
, height
);
136 this.listener
= listener
;
137 eventQueue
= new LinkedList
<TInputEvent
>();
138 sessionInfo
= new TSessionInfo(width
, height
);
139 otherScreen
= new OtherScreen(this);
140 otherScreen
.setDimensions(width
- 2, height
- 2);
141 drawLock
= otherScreen
;
142 setHiddenMouse(true);
146 * Public constructor. Window will be located at (0, 0).
148 * @param listener the object this backend needs to wake up when new
150 * @param application TApplication that manages this window
151 * @param title window title, will be centered along the top border
152 * @param width width of window
153 * @param height height of window
154 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
156 public TWindowBackend(final Object listener
,
157 final TApplication application
, final String title
,
158 final int width
, final int height
, final int flags
) {
160 super(application
, title
, width
, height
, flags
);
162 this.listener
= listener
;
163 eventQueue
= new LinkedList
<TInputEvent
>();
164 sessionInfo
= new TSessionInfo(width
, height
);
165 otherScreen
= new OtherScreen(this);
166 otherScreen
.setDimensions(width
- 2, height
- 2);
167 drawLock
= otherScreen
;
168 setHiddenMouse(true);
172 * Public constructor.
174 * @param listener the object this backend needs to wake up when new
176 * @param application TApplication that manages this window
177 * @param title window title, will be centered along the top border
178 * @param x column relative to parent
179 * @param y row relative to parent
180 * @param width width of window
181 * @param height height of window
183 public TWindowBackend(final Object listener
,
184 final TApplication application
, final String title
,
185 final int x
, final int y
, final int width
, final int height
) {
187 super(application
, title
, x
, y
, width
, height
);
189 this.listener
= listener
;
190 eventQueue
= new LinkedList
<TInputEvent
>();
191 sessionInfo
= new TSessionInfo(width
, height
);
192 otherScreen
= new OtherScreen(this);
193 otherScreen
.setDimensions(width
- 2, height
- 2);
194 drawLock
= otherScreen
;
195 setHiddenMouse(true);
199 * Public constructor.
201 * @param listener the object this backend needs to wake up when new
203 * @param application TApplication that manages this window
204 * @param title window title, will be centered along the top border
205 * @param x column relative to parent
206 * @param y row relative to parent
207 * @param width width of window
208 * @param height height of window
209 * @param flags mask of RESIZABLE, CENTERED, or MODAL
211 public TWindowBackend(final Object listener
,
212 final TApplication application
, final String title
,
213 final int x
, final int y
, final int width
, final int height
,
216 super(application
, title
, x
, y
, width
, height
, flags
);
218 this.listener
= listener
;
219 eventQueue
= new LinkedList
<TInputEvent
>();
220 sessionInfo
= new TSessionInfo(width
, height
);
221 otherScreen
= new OtherScreen(this);
222 otherScreen
.setDimensions(width
- 2, height
- 2);
223 drawLock
= otherScreen
;
224 setHiddenMouse(true);
227 // ------------------------------------------------------------------------
228 // Event handlers ---------------------------------------------------------
229 // ------------------------------------------------------------------------
232 * Handle window/screen resize events.
234 * @param event resize event
237 public void onResize(final TResizeEvent event
) {
238 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
239 int newWidth
= event
.getWidth() - 2;
240 int newHeight
= event
.getHeight() - 2;
241 if ((newWidth
!= otherScreen
.getWidth())
242 || (newHeight
!= otherScreen
.getHeight())
244 // I was resized, notify the screen I am watching to match my
246 synchronized (eventQueue
) {
247 eventQueue
.add(new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
248 newWidth
, newHeight
));
250 synchronized (listener
) {
251 listener
.notifyAll();
256 super.onResize(event
);
261 * Returns true if the mouse is currently in the otherScreen window.
263 * @param mouse mouse event
264 * @return true if mouse is currently in the otherScreen window.
266 protected boolean mouseOnOtherScreen(final TMouseEvent mouse
) {
267 if ((mouse
.getY() >= 1)
268 && (mouse
.getY() <= otherScreen
.getHeight())
269 && (mouse
.getX() >= 1)
270 && (mouse
.getX() <= otherScreen
.getWidth())
278 * Handle mouse button presses.
280 * @param mouse mouse button event
283 public void onMouseDown(final TMouseEvent mouse
) {
284 if (mouseOnOtherScreen(mouse
)) {
285 TMouseEvent event
= mouse
.dup();
286 event
.setX(mouse
.getX() - 1);
287 event
.setY(mouse
.getY() - 1);
288 event
.setAbsoluteX(event
.getX());
289 event
.setAbsoluteY(event
.getY());
290 synchronized (eventQueue
) {
291 eventQueue
.add(event
);
293 synchronized (listener
) {
294 listener
.notifyAll();
297 super.onMouseDown(mouse
);
301 * Handle mouse button releases.
303 * @param mouse mouse button release event
306 public void onMouseUp(final TMouseEvent mouse
) {
307 if (mouseOnOtherScreen(mouse
)) {
308 TMouseEvent event
= mouse
.dup();
309 event
.setX(mouse
.getX() - 1);
310 event
.setY(mouse
.getY() - 1);
311 event
.setAbsoluteX(event
.getX());
312 event
.setAbsoluteY(event
.getY());
313 synchronized (eventQueue
) {
314 eventQueue
.add(event
);
316 synchronized (listener
) {
317 listener
.notifyAll();
320 super.onMouseUp(mouse
);
324 * Handle mouse movements.
326 * @param mouse mouse motion event
329 public void onMouseMotion(final TMouseEvent mouse
) {
330 if (mouseOnOtherScreen(mouse
)) {
331 TMouseEvent event
= mouse
.dup();
332 event
.setX(mouse
.getX() - 1);
333 event
.setY(mouse
.getY() - 1);
334 event
.setAbsoluteX(event
.getX());
335 event
.setAbsoluteY(event
.getY());
336 synchronized (eventQueue
) {
337 eventQueue
.add(event
);
339 synchronized (listener
) {
340 listener
.notifyAll();
343 super.onMouseMotion(mouse
);
349 * @param keypress keystroke event
352 public void onKeypress(final TKeypressEvent keypress
) {
353 TKeypressEvent event
= keypress
.dup();
354 synchronized (eventQueue
) {
355 eventQueue
.add(event
);
357 synchronized (listener
) {
358 listener
.notifyAll();
362 // ------------------------------------------------------------------------
363 // TWindow ----------------------------------------------------------------
364 // ------------------------------------------------------------------------
367 * Draw the foreground colors grid.
372 // Sync on other screen, so that we do not draw in the middle of
373 // their screen update.
374 synchronized (drawLock
) {
378 // Draw every cell of the other screen
379 for (int y
= 0; y
< otherScreen
.getHeight(); y
++) {
380 for (int x
= 0; x
< otherScreen
.getWidth(); x
++) {
381 putCharXY(x
+ 1, y
+ 1, otherScreen
.getCharXY(x
, y
));
385 // If their cursor is visible, draw that here too.
386 if (otherScreen
.isCursorVisible()) {
387 setCursorX(otherScreen
.getCursorX() + 1);
388 setCursorY(otherScreen
.getCursorY() + 1);
389 setCursorVisible(true);
391 setCursorVisible(false);
395 // Check if the other application has died. If so, unset hidden
397 if (otherApplication
!= null) {
398 if (otherApplication
.isRunning() == false) {
399 setHiddenMouse(false);
406 * Subclasses should override this method to cleanup resources. This is
407 * called by application.closeWindow().
410 public void onClose() {
411 // TODO: send a screen disconnect
414 // ------------------------------------------------------------------------
415 // Backend ----------------------------------------------------------------
416 // ------------------------------------------------------------------------
419 * Getter for sessionInfo.
421 * @return the SessionInfo
423 public final SessionInfo
getSessionInfo() {
428 * Subclasses must provide an implementation that syncs the logical
429 * screen to the physical device.
431 public void flushScreen() {
432 getApplication().doRepaint();
436 * Check if there are events in the queue.
438 * @return if true, getEvents() has something to return to the application
440 public boolean hasEvents() {
441 synchronized (eventQueue
) {
442 return (eventQueue
.size() > 0);
447 * Subclasses must provide an implementation to get keyboard, mouse, and
448 * screen resize events.
450 * @param queue list to append new events to
452 public void getEvents(List
<TInputEvent
> queue
) {
453 synchronized (eventQueue
) {
454 if (eventQueue
.size() > 0) {
455 synchronized (queue
) {
456 queue
.addAll(eventQueue
);
464 * Subclasses must provide an implementation that closes sockets,
465 * restores console, etc.
467 public void shutdown() {
472 * Set listener to a different Object.
474 * @param listener the new listening object that run() wakes up on new
477 public void setListener(final Object listener
) {
478 this.listener
= listener
;
482 * Reload backend options from System properties.
484 public void reloadOptions() {
488 // ------------------------------------------------------------------------
489 // TWindowBackend ---------------------------------------------------------
490 // ------------------------------------------------------------------------
493 * Set the object to sync to in draw().
495 * @param drawLock the object to synchronize on
497 public void setDrawLock(final Object drawLock
) {
498 this.drawLock
= drawLock
;
502 * Getter for the other application's screen.
506 public Screen
getOtherScreen() {
511 * Set the other screen's application.
513 * @param application the application driving the other screen
515 public void setOtherApplication(final TApplication application
) {
516 this.otherApplication
= application
;