Fix comment
[fanfix.git] / src / jexer / backend / TWindowBackend.java
CommitLineData
3e074355
KL
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
3e074355
KL
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 */
29package jexer.backend;
30
31import java.util.LinkedList;
32import java.util.List;
33
3e074355
KL
34import jexer.event.TInputEvent;
35import jexer.event.TKeypressEvent;
36import jexer.event.TMouseEvent;
9696a8f6 37import jexer.event.TResizeEvent;
3e074355
KL
38import jexer.TApplication;
39import jexer.TWindow;
40
41/**
42 * TWindowBackend uses a window in one TApplication to provide a backend for
43 * another TApplication.
44 *
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.
48 */
49public class TWindowBackend extends TWindow implements Backend {
50
d36057df
KL
51 // ------------------------------------------------------------------------
52 // Variables --------------------------------------------------------------
53 // ------------------------------------------------------------------------
54
3e074355
KL
55 /**
56 * The listening object that run() wakes up on new input.
57 */
58 private Object listener;
59
60 /**
61 * The object to sync on in draw(). This is normally otherScreen, but it
62 * could also be a MultiScreen.
63 */
64 private Object drawLock;
65
66 /**
67 * The event queue, filled up by a thread reading on input.
68 */
69 private List<TInputEvent> eventQueue;
70
71 /**
9696a8f6 72 * The screen this window is monitoring.
3e074355
KL
73 */
74 private Screen otherScreen;
75
9696a8f6
KL
76 /**
77 * The application associated with otherScreen.
78 */
79 private TApplication otherApplication;
80
3e074355
KL
81 /**
82 * The session information.
83 */
84 private SessionInfo sessionInfo;
85
9696a8f6
KL
86 /**
87 * OtherScreen provides a hook to notify TWindowBackend of screen size
88 * changes.
89 */
90 private class OtherScreen extends LogicalScreen {
91
92 /**
93 * The TWindowBackend to notify.
94 */
95 private TWindowBackend window;
96
97 /**
98 * Public constructor.
99 */
100 public OtherScreen(final TWindowBackend window) {
101 this.window = window;
102 }
103
104 /**
105 * Resize the physical screen to match the logical screen dimensions.
106 */
107 @Override
108 public void resizeToScreen() {
109 window.setWidth(getWidth() + 2);
110 window.setHeight(getHeight() + 2);
111 }
112
113 }
114
115
d36057df
KL
116 // ------------------------------------------------------------------------
117 // Constructors -----------------------------------------------------------
118 // ------------------------------------------------------------------------
3e074355
KL
119
120 /**
121 * Public constructor. Window will be located at (0, 0).
122 *
123 * @param listener the object this backend needs to wake up when new
124 * input comes in
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
129 */
130 public TWindowBackend(final Object listener,
131 final TApplication application, final String title,
132 final int width, final int height) {
133
134 super(application, title, width, height);
135
136 this.listener = listener;
137 eventQueue = new LinkedList<TInputEvent>();
138 sessionInfo = new TSessionInfo(width, height);
9696a8f6 139 otherScreen = new OtherScreen(this);
3e074355
KL
140 otherScreen.setDimensions(width - 2, height - 2);
141 drawLock = otherScreen;
00691e80 142 setHiddenMouse(true);
3e074355
KL
143 }
144
145 /**
146 * Public constructor. Window will be located at (0, 0).
147 *
148 * @param listener the object this backend needs to wake up when new
149 * input comes in
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
155 */
156 public TWindowBackend(final Object listener,
157 final TApplication application, final String title,
158 final int width, final int height, final int flags) {
159
160 super(application, title, width, height, flags);
161
162 this.listener = listener;
163 eventQueue = new LinkedList<TInputEvent>();
164 sessionInfo = new TSessionInfo(width, height);
9696a8f6 165 otherScreen = new OtherScreen(this);
3e074355
KL
166 otherScreen.setDimensions(width - 2, height - 2);
167 drawLock = otherScreen;
00691e80 168 setHiddenMouse(true);
3e074355
KL
169 }
170
171 /**
172 * Public constructor.
173 *
174 * @param listener the object this backend needs to wake up when new
175 * input comes in
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
182 */
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) {
186
187 super(application, title, x, y, width, height);
188
189 this.listener = listener;
190 eventQueue = new LinkedList<TInputEvent>();
191 sessionInfo = new TSessionInfo(width, height);
9696a8f6 192 otherScreen = new OtherScreen(this);
3e074355
KL
193 otherScreen.setDimensions(width - 2, height - 2);
194 drawLock = otherScreen;
00691e80 195 setHiddenMouse(true);
3e074355
KL
196 }
197
198 /**
199 * Public constructor.
200 *
201 * @param listener the object this backend needs to wake up when new
202 * input comes in
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
210 */
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,
214 final int flags) {
215
216 super(application, title, x, y, width, height, flags);
217
218 this.listener = listener;
219 eventQueue = new LinkedList<TInputEvent>();
220 sessionInfo = new TSessionInfo(width, height);
9696a8f6 221 otherScreen = new OtherScreen(this);
3e074355
KL
222 otherScreen.setDimensions(width - 2, height - 2);
223 drawLock = otherScreen;
00691e80 224 setHiddenMouse(true);
3e074355
KL
225 }
226
d36057df
KL
227 // ------------------------------------------------------------------------
228 // Event handlers ---------------------------------------------------------
229 // ------------------------------------------------------------------------
3e074355 230
9696a8f6
KL
231 /**
232 * Handle window/screen resize events.
233 *
234 * @param event resize event
235 */
236 @Override
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())
243 ) {
244 // I was resized, notify the screen I am watching to match my
245 // new size.
246 synchronized (eventQueue) {
247 eventQueue.add(new TResizeEvent(TResizeEvent.Type.SCREEN,
248 newWidth, newHeight));
249 }
250 synchronized (listener) {
251 listener.notifyAll();
252 }
253 }
254 return;
255 } else {
256 super.onResize(event);
257 }
258 }
259
3e074355
KL
260 /**
261 * Returns true if the mouse is currently in the otherScreen window.
262 *
263 * @param mouse mouse event
264 * @return true if mouse is currently in the otherScreen window.
265 */
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())
271 ) {
272 return true;
273 }
274 return false;
275 }
276
277 /**
278 * Handle mouse button presses.
279 *
280 * @param mouse mouse button event
281 */
282 @Override
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);
292 }
293 synchronized (listener) {
294 listener.notifyAll();
295 }
296 }
297 super.onMouseDown(mouse);
298 }
299
300 /**
301 * Handle mouse button releases.
302 *
303 * @param mouse mouse button release event
304 */
305 @Override
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);
315 }
316 synchronized (listener) {
317 listener.notifyAll();
318 }
319 }
320 super.onMouseUp(mouse);
321 }
322
323 /**
324 * Handle mouse movements.
325 *
326 * @param mouse mouse motion event
327 */
328 @Override
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());
3e074355
KL
336 synchronized (eventQueue) {
337 eventQueue.add(event);
338 }
339 synchronized (listener) {
340 listener.notifyAll();
341 }
3e074355
KL
342 }
343 super.onMouseMotion(mouse);
344 }
345
346 /**
347 * Handle keystrokes.
348 *
349 * @param keypress keystroke event
350 */
351 @Override
352 public void onKeypress(final TKeypressEvent keypress) {
353 TKeypressEvent event = keypress.dup();
354 synchronized (eventQueue) {
355 eventQueue.add(event);
356 }
357 synchronized (listener) {
358 listener.notifyAll();
359 }
360 }
361
d36057df
KL
362 // ------------------------------------------------------------------------
363 // TWindow ----------------------------------------------------------------
364 // ------------------------------------------------------------------------
365
366 /**
367 * Draw the foreground colors grid.
368 */
369 @Override
370 public void draw() {
371
372 // Sync on other screen, so that we do not draw in the middle of
373 // their screen update.
374 synchronized (drawLock) {
375 // Draw the box
376 super.draw();
377
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));
382 }
383 }
384
d36057df
KL
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);
390 } else {
391 setCursorVisible(false);
392 }
393 }
9696a8f6
KL
394
395 // Check if the other application has died. If so, unset hidden
396 // mouse.
397 if (otherApplication != null) {
398 if (otherApplication.isRunning() == false) {
399 setHiddenMouse(false);
400 }
401 }
402
d36057df
KL
403 }
404
405 /**
406 * Subclasses should override this method to cleanup resources. This is
407 * called by application.closeWindow().
408 */
409 @Override
410 public void onClose() {
411 // TODO: send a screen disconnect
412 }
413
414 // ------------------------------------------------------------------------
415 // Backend ----------------------------------------------------------------
416 // ------------------------------------------------------------------------
417
418 /**
419 * Getter for sessionInfo.
420 *
421 * @return the SessionInfo
422 */
423 public final SessionInfo getSessionInfo() {
424 return sessionInfo;
425 }
426
427 /**
428 * Subclasses must provide an implementation that syncs the logical
429 * screen to the physical device.
430 */
431 public void flushScreen() {
432 getApplication().doRepaint();
433 }
434
435 /**
436 * Check if there are events in the queue.
437 *
438 * @return if true, getEvents() has something to return to the application
439 */
440 public boolean hasEvents() {
441 synchronized (eventQueue) {
442 return (eventQueue.size() > 0);
443 }
444 }
445
446 /**
447 * Subclasses must provide an implementation to get keyboard, mouse, and
448 * screen resize events.
449 *
450 * @param queue list to append new events to
451 */
452 public void getEvents(List<TInputEvent> queue) {
453 synchronized (eventQueue) {
454 if (eventQueue.size() > 0) {
455 synchronized (queue) {
456 queue.addAll(eventQueue);
457 }
458 eventQueue.clear();
459 }
460 }
461 }
462
463 /**
464 * Subclasses must provide an implementation that closes sockets,
465 * restores console, etc.
466 */
467 public void shutdown() {
468 // NOP
469 }
470
471 /**
472 * Set listener to a different Object.
473 *
474 * @param listener the new listening object that run() wakes up on new
475 * input
476 */
477 public void setListener(final Object listener) {
478 this.listener = listener;
479 }
480
e23ea538
KL
481 /**
482 * Reload backend options from System properties.
483 */
484 public void reloadOptions() {
485 // NOP
486 }
487
d36057df
KL
488 // ------------------------------------------------------------------------
489 // TWindowBackend ---------------------------------------------------------
490 // ------------------------------------------------------------------------
491
492 /**
493 * Set the object to sync to in draw().
494 *
495 * @param drawLock the object to synchronize on
496 */
497 public void setDrawLock(final Object drawLock) {
498 this.drawLock = drawLock;
499 }
500
501 /**
502 * Getter for the other application's screen.
503 *
504 * @return the Screen
505 */
506 public Screen getOtherScreen() {
507 return otherScreen;
508 }
509
9696a8f6
KL
510 /**
511 * Set the other screen's application.
512 *
513 * @param application the application driving the other screen
514 */
515 public void setOtherApplication(final TApplication application) {
516 this.otherApplication = application;
517 }
518
3e074355 519}