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
.ArrayList
;
32 import java
.util
.List
;
34 import jexer
.TApplication
;
36 import jexer
.event
.TCommandEvent
;
37 import jexer
.event
.TInputEvent
;
38 import jexer
.event
.TKeypressEvent
;
39 import jexer
.event
.TMouseEvent
;
40 import jexer
.event
.TResizeEvent
;
41 import static jexer
.TCommand
.*;
44 * TWindowBackend uses a window in one TApplication to provide a backend for
45 * another TApplication.
47 * Note that TWindow has its own getScreen() and setTitle() functions.
48 * Clients in TWindowBackend's application won't be able to use it to get at
49 * the other application's screen. getOtherScreen() has been provided.
51 public class TWindowBackend
extends TWindow
implements Backend
{
53 // ------------------------------------------------------------------------
54 // Variables --------------------------------------------------------------
55 // ------------------------------------------------------------------------
58 * The listening object that run() wakes up on new input.
60 private Object listener
;
63 * The object to sync on in draw(). This is normally otherScreen, but it
64 * could also be a MultiScreen.
66 private Object drawLock
;
69 * The event queue, filled up by a thread reading on input.
71 private List
<TInputEvent
> eventQueue
;
74 * The screen this window is monitoring.
76 private Screen otherScreen
;
79 * The application associated with otherScreen.
81 private TApplication otherApplication
;
84 * The session information.
86 private SessionInfo sessionInfo
;
89 * OtherScreen provides a hook to notify TWindowBackend of screen size
92 private class OtherScreen
extends LogicalScreen
{
95 * The TWindowBackend to notify.
97 private TWindowBackend window
;
100 * Public constructor.
102 public OtherScreen(final TWindowBackend window
) {
103 this.window
= window
;
107 * Resize the physical screen to match the logical screen dimensions.
110 public void resizeToScreen() {
111 window
.setWidth(getWidth() + 2);
112 window
.setHeight(getHeight() + 2);
118 // ------------------------------------------------------------------------
119 // Constructors -----------------------------------------------------------
120 // ------------------------------------------------------------------------
123 * Public constructor. Window will be located at (0, 0).
125 * @param listener the object this backend needs to wake up when new
127 * @param application TApplication that manages this window
128 * @param title window title, will be centered along the top border
129 * @param width width of window
130 * @param height height of window
132 public TWindowBackend(final Object listener
,
133 final TApplication application
, final String title
,
134 final int width
, final int height
) {
136 super(application
, title
, width
, height
);
138 this.listener
= listener
;
139 eventQueue
= new ArrayList
<TInputEvent
>();
140 sessionInfo
= new TSessionInfo(width
, height
);
141 otherScreen
= new OtherScreen(this);
142 otherScreen
.setDimensions(width
- 2, height
- 2);
143 drawLock
= otherScreen
;
144 setHiddenMouse(true);
148 * Public constructor. Window will be located at (0, 0).
150 * @param listener the object this backend needs to wake up when new
152 * @param application TApplication that manages this window
153 * @param title window title, will be centered along the top border
154 * @param width width of window
155 * @param height height of window
156 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
158 public TWindowBackend(final Object listener
,
159 final TApplication application
, final String title
,
160 final int width
, final int height
, final int flags
) {
162 super(application
, title
, width
, height
, flags
);
164 this.listener
= listener
;
165 eventQueue
= new ArrayList
<TInputEvent
>();
166 sessionInfo
= new TSessionInfo(width
, height
);
167 otherScreen
= new OtherScreen(this);
168 otherScreen
.setDimensions(width
- 2, height
- 2);
169 drawLock
= otherScreen
;
170 setHiddenMouse(true);
174 * Public constructor.
176 * @param listener the object this backend needs to wake up when new
178 * @param application TApplication that manages this window
179 * @param title window title, will be centered along the top border
180 * @param x column relative to parent
181 * @param y row relative to parent
182 * @param width width of window
183 * @param height height of window
185 public TWindowBackend(final Object listener
,
186 final TApplication application
, final String title
,
187 final int x
, final int y
, final int width
, final int height
) {
189 super(application
, title
, x
, y
, width
, height
);
191 this.listener
= listener
;
192 eventQueue
= new ArrayList
<TInputEvent
>();
193 sessionInfo
= new TSessionInfo(width
, height
);
194 otherScreen
= new OtherScreen(this);
195 otherScreen
.setDimensions(width
- 2, height
- 2);
196 drawLock
= otherScreen
;
197 setHiddenMouse(true);
201 * Public constructor.
203 * @param listener the object this backend needs to wake up when new
205 * @param application TApplication that manages this window
206 * @param title window title, will be centered along the top border
207 * @param x column relative to parent
208 * @param y row relative to parent
209 * @param width width of window
210 * @param height height of window
211 * @param flags mask of RESIZABLE, CENTERED, or MODAL
213 public TWindowBackend(final Object listener
,
214 final TApplication application
, final String title
,
215 final int x
, final int y
, final int width
, final int height
,
218 super(application
, title
, x
, y
, width
, height
, flags
);
220 this.listener
= listener
;
221 eventQueue
= new ArrayList
<TInputEvent
>();
222 sessionInfo
= new TSessionInfo(width
, height
);
223 otherScreen
= new OtherScreen(this);
224 otherScreen
.setDimensions(width
- 2, height
- 2);
225 drawLock
= otherScreen
;
226 setHiddenMouse(true);
229 // ------------------------------------------------------------------------
230 // Event handlers ---------------------------------------------------------
231 // ------------------------------------------------------------------------
234 * Handle window/screen resize events.
236 * @param event resize event
239 public void onResize(final TResizeEvent event
) {
240 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
241 int newWidth
= event
.getWidth() - 2;
242 int newHeight
= event
.getHeight() - 2;
243 if ((newWidth
!= otherScreen
.getWidth())
244 || (newHeight
!= otherScreen
.getHeight())
246 // I was resized, notify the screen I am watching to match my
248 synchronized (eventQueue
) {
249 eventQueue
.add(new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
250 newWidth
, newHeight
));
252 synchronized (listener
) {
253 listener
.notifyAll();
258 super.onResize(event
);
263 * Returns true if the mouse is currently in the otherScreen window.
265 * @param mouse mouse event
266 * @return true if mouse is currently in the otherScreen window.
268 protected boolean mouseOnOtherScreen(final TMouseEvent mouse
) {
269 if ((mouse
.getY() >= 1)
270 && (mouse
.getY() <= otherScreen
.getHeight())
271 && (mouse
.getX() >= 1)
272 && (mouse
.getX() <= otherScreen
.getWidth())
280 * Handle mouse button presses.
282 * @param mouse mouse button event
285 public void onMouseDown(final TMouseEvent mouse
) {
286 if (mouseOnOtherScreen(mouse
)) {
287 TMouseEvent event
= mouse
.dup();
288 event
.setX(mouse
.getX() - 1);
289 event
.setY(mouse
.getY() - 1);
290 event
.setAbsoluteX(event
.getX());
291 event
.setAbsoluteY(event
.getY());
292 synchronized (eventQueue
) {
293 eventQueue
.add(event
);
295 synchronized (listener
) {
296 listener
.notifyAll();
299 super.onMouseDown(mouse
);
303 * Handle mouse button releases.
305 * @param mouse mouse button release event
308 public void onMouseUp(final TMouseEvent mouse
) {
309 if (mouseOnOtherScreen(mouse
)) {
310 TMouseEvent event
= mouse
.dup();
311 event
.setX(mouse
.getX() - 1);
312 event
.setY(mouse
.getY() - 1);
313 event
.setAbsoluteX(event
.getX());
314 event
.setAbsoluteY(event
.getY());
315 synchronized (eventQueue
) {
316 eventQueue
.add(event
);
318 synchronized (listener
) {
319 listener
.notifyAll();
322 super.onMouseUp(mouse
);
326 * Handle mouse movements.
328 * @param mouse mouse motion event
331 public void onMouseMotion(final TMouseEvent mouse
) {
332 if (mouseOnOtherScreen(mouse
)) {
333 TMouseEvent event
= mouse
.dup();
334 event
.setX(mouse
.getX() - 1);
335 event
.setY(mouse
.getY() - 1);
336 event
.setAbsoluteX(event
.getX());
337 event
.setAbsoluteY(event
.getY());
338 synchronized (eventQueue
) {
339 eventQueue
.add(event
);
341 synchronized (listener
) {
342 listener
.notifyAll();
345 super.onMouseMotion(mouse
);
351 * @param keypress keystroke event
354 public void onKeypress(final TKeypressEvent keypress
) {
355 TKeypressEvent event
= keypress
.dup();
356 synchronized (eventQueue
) {
357 eventQueue
.add(event
);
359 synchronized (listener
) {
360 listener
.notifyAll();
364 // ------------------------------------------------------------------------
365 // TWindow ----------------------------------------------------------------
366 // ------------------------------------------------------------------------
369 * Draw the foreground colors grid.
374 // Sync on other screen, so that we do not draw in the middle of
375 // their screen update.
376 synchronized (drawLock
) {
380 // Draw every cell of the other screen
381 for (int y
= 0; y
< otherScreen
.getHeight(); y
++) {
382 for (int x
= 0; x
< otherScreen
.getWidth(); x
++) {
383 putCharXY(x
+ 1, y
+ 1, otherScreen
.getCharXY(x
, y
));
387 // If their cursor is visible, draw that here too.
388 if (otherScreen
.isCursorVisible()) {
389 setCursorX(otherScreen
.getCursorX() + 1);
390 setCursorY(otherScreen
.getCursorY() + 1);
391 setCursorVisible(true);
393 setCursorVisible(false);
397 // Check if the other application has died. If so, unset hidden
399 if (otherApplication
!= null) {
400 if (otherApplication
.isRunning() == false) {
401 setHiddenMouse(false);
408 * Subclasses should override this method to cleanup resources. This is
409 * called by application.closeWindow().
412 public void onClose() {
413 synchronized (eventQueue
) {
414 eventQueue
.add(new TCommandEvent(cmBackendDisconnect
));
418 // ------------------------------------------------------------------------
419 // Backend ----------------------------------------------------------------
420 // ------------------------------------------------------------------------
423 * Getter for sessionInfo.
425 * @return the SessionInfo
427 public final SessionInfo
getSessionInfo() {
432 * Subclasses must provide an implementation that syncs the logical
433 * screen to the physical device.
435 public void flushScreen() {
436 getApplication().doRepaint();
440 * Check if there are events in the queue.
442 * @return if true, getEvents() has something to return to the application
444 public boolean hasEvents() {
445 synchronized (eventQueue
) {
446 return (eventQueue
.size() > 0);
451 * Subclasses must provide an implementation to get keyboard, mouse, and
452 * screen resize events.
454 * @param queue list to append new events to
456 public void getEvents(List
<TInputEvent
> queue
) {
457 synchronized (eventQueue
) {
458 if (eventQueue
.size() > 0) {
459 synchronized (queue
) {
460 queue
.addAll(eventQueue
);
468 * Subclasses must provide an implementation that closes sockets,
469 * restores console, etc.
471 public void shutdown() {
476 * Set listener to a different Object.
478 * @param listener the new listening object that run() wakes up on new
481 public void setListener(final Object listener
) {
482 this.listener
= listener
;
486 * Reload backend options from System properties.
488 public void reloadOptions() {
492 // ------------------------------------------------------------------------
493 // TWindowBackend ---------------------------------------------------------
494 // ------------------------------------------------------------------------
497 * Set the object to sync to in draw().
499 * @param drawLock the object to synchronize on
501 public void setDrawLock(final Object drawLock
) {
502 this.drawLock
= drawLock
;
506 * Getter for the other application's screen.
510 public Screen
getOtherScreen() {
515 * Set the other screen's application.
517 * @param application the application driving the other screen
519 public void setOtherApplication(final TApplication application
) {
520 this.otherApplication
= application
;