2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
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.
10 * Copyright (C) 2015 Kevin Lamonte
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.
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.
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
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import java
.awt
.event
.ComponentEvent
;
34 import java
.awt
.event
.ComponentListener
;
35 import java
.awt
.event
.KeyEvent
;
36 import java
.awt
.event
.KeyListener
;
37 import java
.awt
.event
.MouseEvent
;
38 import java
.awt
.event
.MouseListener
;
39 import java
.awt
.event
.MouseMotionListener
;
40 import java
.awt
.event
.MouseWheelEvent
;
41 import java
.awt
.event
.MouseWheelListener
;
42 import java
.awt
.event
.WindowEvent
;
43 import java
.awt
.event
.WindowListener
;
44 import java
.util
.List
;
45 import java
.util
.LinkedList
;
47 import jexer
.TKeypress
;
48 import jexer
.event
.TCommandEvent
;
49 import jexer
.event
.TInputEvent
;
50 import jexer
.event
.TKeypressEvent
;
51 import jexer
.event
.TMouseEvent
;
52 import jexer
.event
.TResizeEvent
;
53 import jexer
.session
.SessionInfo
;
54 import jexer
.session
.SwingSessionInfo
;
55 import static jexer
.TCommand
.*;
56 import static jexer
.TKeypress
.*;
59 * This class reads keystrokes and mouse events from an Swing JFrame.
61 public final class SwingTerminal
implements ComponentListener
, KeyListener
,
62 MouseListener
, MouseMotionListener
,
63 MouseWheelListener
, WindowListener
{
68 private SwingScreen screen
;
71 * The session information.
73 private SwingSessionInfo sessionInfo
;
76 * Getter for sessionInfo.
78 * @return the SessionInfo
80 public SessionInfo
getSessionInfo() {
85 * The listening object that run() wakes up on new input.
87 private Object listener
;
90 * The event queue, filled up by a thread reading on input.
92 private List
<TInputEvent
> eventQueue
;
95 * The last reported mouse X position.
97 private int oldMouseX
= -1;
100 * The last reported mouse Y position.
102 private int oldMouseY
= -1;
105 * true if mouse1 was down. Used to report mouse1 on the release event.
107 private boolean mouse1
= false;
110 * true if mouse2 was down. Used to report mouse2 on the release event.
112 private boolean mouse2
= false;
115 * true if mouse3 was down. Used to report mouse3 on the release event.
117 private boolean mouse3
= false;
120 * Check if there are events in the queue.
122 * @return if true, getEvents() has something to return to the backend
124 public boolean hasEvents() {
125 synchronized (eventQueue
) {
126 return (eventQueue
.size() > 0);
131 * Constructor sets up state for getEvent().
133 * @param listener the object this backend needs to wake up when new
135 * @param screen the top-level Swing frame
137 public SwingTerminal(final Object listener
, final SwingScreen screen
) {
138 this.listener
= listener
;
139 this.screen
= screen
;
143 sessionInfo
= screen
.getSessionInfo();
144 eventQueue
= new LinkedList
<TInputEvent
>();
146 screen
.frame
.addKeyListener(this);
147 screen
.frame
.addWindowListener(this);
148 screen
.frame
.addComponentListener(this);
149 screen
.frame
.addMouseListener(this);
150 screen
.frame
.addMouseMotionListener(this);
151 screen
.frame
.addMouseWheelListener(this);
155 * Return any events in the IO queue.
157 * @param queue list to append new events to
159 public void getEvents(final List
<TInputEvent
> queue
) {
160 synchronized (eventQueue
) {
161 if (eventQueue
.size() > 0) {
162 synchronized (queue
) {
163 queue
.addAll(eventQueue
);
171 * Pass Swing keystrokes into the event queue.
173 * @param key keystroke received
176 public void keyReleased(final KeyEvent key
) {
177 // Ignore release events
181 * Pass Swing keystrokes into the event queue.
183 * @param key keystroke received
186 public void keyTyped(final KeyEvent key
) {
187 // Ignore typed events
191 * Pass Swing keystrokes into the event queue.
193 * @param key keystroke received
196 public void keyPressed(final KeyEvent key
) {
198 boolean shift
= false;
199 boolean ctrl
= false;
201 boolean isKey
= false;
202 if (key
.isActionKey()) {
205 ch
= key
.getKeyChar();
207 alt
= key
.isAltDown();
208 ctrl
= key
.isControlDown();
209 shift
= key
.isShiftDown();
212 System.err.printf("Swing Key: %s\n", key);
213 System.err.printf(" isKey: %s\n", isKey);
214 System.err.printf(" alt: %s\n", alt);
215 System.err.printf(" ctrl: %s\n", ctrl);
216 System.err.printf(" shift: %s\n", shift);
217 System.err.printf(" ch: %s\n", ch);
220 // Special case: not return the bare modifier presses
221 switch (key
.getKeyCode()) {
222 case KeyEvent
.VK_ALT
:
224 case KeyEvent
.VK_ALT_GRAPH
:
226 case KeyEvent
.VK_CONTROL
:
228 case KeyEvent
.VK_SHIFT
:
230 case KeyEvent
.VK_META
:
236 TKeypress keypress
= null;
238 switch (key
.getKeyCode()) {
240 keypress
= new TKeypress(true, TKeypress
.F1
, ' ',
244 keypress
= new TKeypress(true, TKeypress
.F2
, ' ',
248 keypress
= new TKeypress(true, TKeypress
.F3
, ' ',
252 keypress
= new TKeypress(true, TKeypress
.F4
, ' ',
256 keypress
= new TKeypress(true, TKeypress
.F5
, ' ',
260 keypress
= new TKeypress(true, TKeypress
.F6
, ' ',
264 keypress
= new TKeypress(true, TKeypress
.F7
, ' ',
268 keypress
= new TKeypress(true, TKeypress
.F8
, ' ',
272 keypress
= new TKeypress(true, TKeypress
.F9
, ' ',
275 case KeyEvent
.VK_F10
:
276 keypress
= new TKeypress(true, TKeypress
.F10
, ' ',
279 case KeyEvent
.VK_F11
:
280 keypress
= new TKeypress(true, TKeypress
.F11
, ' ',
283 case KeyEvent
.VK_F12
:
284 keypress
= new TKeypress(true, TKeypress
.F12
, ' ',
287 case KeyEvent
.VK_HOME
:
288 keypress
= new TKeypress(true, TKeypress
.HOME
, ' ',
291 case KeyEvent
.VK_END
:
292 keypress
= new TKeypress(true, TKeypress
.END
, ' ',
295 case KeyEvent
.VK_PAGE_UP
:
296 keypress
= new TKeypress(true, TKeypress
.PGUP
, ' ',
299 case KeyEvent
.VK_PAGE_DOWN
:
300 keypress
= new TKeypress(true, TKeypress
.PGDN
, ' ',
303 case KeyEvent
.VK_INSERT
:
304 keypress
= new TKeypress(true, TKeypress
.INS
, ' ',
307 case KeyEvent
.VK_DELETE
:
308 keypress
= new TKeypress(true, TKeypress
.DEL
, ' ',
311 case KeyEvent
.VK_RIGHT
:
312 keypress
= new TKeypress(true, TKeypress
.RIGHT
, ' ',
315 case KeyEvent
.VK_LEFT
:
316 keypress
= new TKeypress(true, TKeypress
.LEFT
, ' ',
320 keypress
= new TKeypress(true, TKeypress
.UP
, ' ',
323 case KeyEvent
.VK_DOWN
:
324 keypress
= new TKeypress(true, TKeypress
.DOWN
, ' ',
327 case KeyEvent
.VK_TAB
:
328 // Special case: distinguish TAB vs BTAB
330 keypress
= kbShiftTab
;
335 case KeyEvent
.VK_ENTER
:
336 keypress
= new TKeypress(true, TKeypress
.ENTER
, ' ',
339 case KeyEvent
.VK_ESCAPE
:
340 keypress
= new TKeypress(true, TKeypress
.ESC
, ' ',
343 case KeyEvent
.VK_BACK_SPACE
:
344 // Special case: return it as kbBackspace (Ctrl-H)
345 keypress
= new TKeypress(false, 0, 'H', false, true, false);
348 // Unsupported, ignore
353 if (keypress
== null) {
356 keypress
= kbBackspace
;
369 keypress
= kbShiftTab
;
378 if (!alt
&& ctrl
&& !shift
) {
379 ch
= KeyEvent
.getKeyText(key
.getKeyCode()).charAt(0);
381 // Not a special key, put it together
382 keypress
= new TKeypress(false, 0, ch
, alt
, ctrl
, shift
);
386 // Save it and we are done.
387 synchronized (eventQueue
) {
388 eventQueue
.add(new TKeypressEvent(keypress
));
390 synchronized (listener
) {
391 listener
.notifyAll();
396 * Pass window events into the event queue.
398 * @param event window event received
401 public void windowActivated(final WindowEvent event
) {
402 // Force a total repaint
403 synchronized (screen
) {
404 screen
.clearPhysical();
409 * Pass window events into the event queue.
411 * @param event window event received
414 public void windowClosed(final WindowEvent event
) {
419 * Pass window events into the event queue.
421 * @param event window event received
424 public void windowClosing(final WindowEvent event
) {
425 // Drop a cmAbort and walk away
426 synchronized (eventQueue
) {
427 eventQueue
.add(new TCommandEvent(cmAbort
));
429 synchronized (listener
) {
430 listener
.notifyAll();
435 * Pass window events into the event queue.
437 * @param event window event received
440 public void windowDeactivated(final WindowEvent event
) {
445 * Pass window events into the event queue.
447 * @param event window event received
450 public void windowDeiconified(final WindowEvent event
) {
455 * Pass window events into the event queue.
457 * @param event window event received
460 public void windowIconified(final WindowEvent event
) {
465 * Pass window events into the event queue.
467 * @param event window event received
470 public void windowOpened(final WindowEvent event
) {
475 * Pass component events into the event queue.
477 * @param event component event received
480 public void componentHidden(final ComponentEvent event
) {
485 * Pass component events into the event queue.
487 * @param event component event received
490 public void componentShown(final ComponentEvent event
) {
495 * Pass component events into the event queue.
497 * @param event component event received
500 public void componentMoved(final ComponentEvent event
) {
505 * Pass component events into the event queue.
507 * @param event component event received
510 public void componentResized(final ComponentEvent event
) {
511 // Drop a new TResizeEvent into the queue
512 sessionInfo
.queryWindowSize();
513 synchronized (eventQueue
) {
514 TResizeEvent windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
515 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
516 eventQueue
.add(windowResize
);
518 synchronized (listener
) {
519 listener
.notifyAll();
524 * Pass mouse events into the event queue.
526 * @param mouse mouse event received
529 public void mouseDragged(final MouseEvent mouse
) {
530 int modifiers
= mouse
.getModifiersEx();
531 boolean eventMouse1
= false;
532 boolean eventMouse2
= false;
533 boolean eventMouse3
= false;
534 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
537 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
540 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
543 mouse1
= eventMouse1
;
544 mouse2
= eventMouse2
;
545 mouse3
= eventMouse3
;
546 int x
= screen
.textColumn(mouse
.getX());
547 int y
= screen
.textRow(mouse
.getY());
549 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
550 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
552 synchronized (eventQueue
) {
553 eventQueue
.add(mouseEvent
);
555 synchronized (listener
) {
556 listener
.notifyAll();
561 * Pass mouse events into the event queue.
563 * @param mouse mouse event received
566 public void mouseMoved(final MouseEvent mouse
) {
567 int x
= screen
.textColumn(mouse
.getX());
568 int y
= screen
.textRow(mouse
.getY());
569 if ((x
== oldMouseX
) && (y
== oldMouseY
)) {
570 // Bail out, we've moved some pixels but not a whole text cell.
576 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
577 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
579 synchronized (eventQueue
) {
580 eventQueue
.add(mouseEvent
);
582 synchronized (listener
) {
583 listener
.notifyAll();
588 * Pass mouse events into the event queue.
590 * @param mouse mouse event received
593 public void mouseClicked(final MouseEvent mouse
) {
598 * Pass mouse events into the event queue.
600 * @param mouse mouse event received
603 public void mouseEntered(final MouseEvent mouse
) {
608 * Pass mouse events into the event queue.
610 * @param mouse mouse event received
613 public void mouseExited(final MouseEvent mouse
) {
618 * Pass mouse events into the event queue.
620 * @param mouse mouse event received
623 public void mousePressed(final MouseEvent mouse
) {
624 int modifiers
= mouse
.getModifiersEx();
625 boolean eventMouse1
= false;
626 boolean eventMouse2
= false;
627 boolean eventMouse3
= false;
628 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
631 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
634 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
637 mouse1
= eventMouse1
;
638 mouse2
= eventMouse2
;
639 mouse3
= eventMouse3
;
640 int x
= screen
.textColumn(mouse
.getX());
641 int y
= screen
.textRow(mouse
.getY());
643 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
644 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
646 synchronized (eventQueue
) {
647 eventQueue
.add(mouseEvent
);
649 synchronized (listener
) {
650 listener
.notifyAll();
655 * Pass mouse events into the event queue.
657 * @param mouse mouse event received
660 public void mouseReleased(final MouseEvent mouse
) {
661 int modifiers
= mouse
.getModifiersEx();
662 boolean eventMouse1
= false;
663 boolean eventMouse2
= false;
664 boolean eventMouse3
= false;
665 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
668 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
671 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
686 int x
= screen
.textColumn(mouse
.getX());
687 int y
= screen
.textRow(mouse
.getY());
689 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_UP
,
690 x
, y
, x
, y
, eventMouse1
, eventMouse2
, eventMouse3
, false, false);
692 synchronized (eventQueue
) {
693 eventQueue
.add(mouseEvent
);
695 synchronized (listener
) {
696 listener
.notifyAll();
701 * Pass mouse events into the event queue.
703 * @param mouse mouse event received
706 public void mouseWheelMoved(final MouseWheelEvent mouse
) {
707 int modifiers
= mouse
.getModifiersEx();
708 boolean eventMouse1
= false;
709 boolean eventMouse2
= false;
710 boolean eventMouse3
= false;
711 boolean mouseWheelUp
= false;
712 boolean mouseWheelDown
= false;
713 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
716 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
719 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
722 mouse1
= eventMouse1
;
723 mouse2
= eventMouse2
;
724 mouse3
= eventMouse3
;
725 int x
= screen
.textColumn(mouse
.getX());
726 int y
= screen
.textRow(mouse
.getY());
727 if (mouse
.getWheelRotation() > 0) {
728 mouseWheelDown
= true;
730 if (mouse
.getWheelRotation() < 0) {
734 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
735 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, mouseWheelUp
, mouseWheelDown
);
737 synchronized (eventQueue
) {
738 eventQueue
.add(mouseEvent
);
740 synchronized (listener
) {
741 listener
.notifyAll();