From 81178ca997f143ff5811ac022c94108ea1273b5b Mon Sep 17 00:00:00 2001 From: Diego Waxemberg Date: Thu, 12 Dec 2013 19:45:35 -0500 Subject: [PATCH] holding CTRL + ALT + SHIFT will now release the mouse. Also fixed mouse re-centering issue (i believe cannot test for sure on my system) --- .../src/com/limelight/gui/StreamFrame.java | 65 +++++---- .../com/limelight/input/KeyboardHandler.java | 42 ++++-- .../src/com/limelight/input/MouseHandler.java | 128 ++++++++++-------- 3 files changed, 143 insertions(+), 92 deletions(-) diff --git a/limelight-pc/src/com/limelight/gui/StreamFrame.java b/limelight-pc/src/com/limelight/gui/StreamFrame.java index ea81294..f8995b6 100644 --- a/limelight-pc/src/com/limelight/gui/StreamFrame.java +++ b/limelight-pc/src/com/limelight/gui/StreamFrame.java @@ -26,23 +26,33 @@ import com.limelight.nvstream.NvConnectionListener.Stage; public class StreamFrame extends JFrame { private static final long serialVersionUID = 1L; - + private int centerX; private int centerY; private KeyboardHandler keyboard; private MouseHandler mouse; private JProgressBar spinner; private JLabel spinnerLabel; - + private Cursor noCursor; private NvConnection conn; - + public StreamFrame() { Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); this.setSize(1280,720); centerX = dim.width/2-this.getSize().width/2; centerY = dim.height/2-this.getSize().height/2; } - + + public void freeMouse() { + mouse.free(); + showCursor(); + } + + public void captureMouse() { + mouse.capture(); + hideCursor(); + } + public void build(NvConnection conn, boolean fullscreen) { this.conn = conn; @@ -59,11 +69,11 @@ public class StreamFrame extends JFrame { this.setLocation(centerX, 0); this.setBackground(Color.BLACK); - + if (fullscreen) { makeFullScreen(); } - + hideCursor(); this.setVisible(true); } @@ -84,59 +94,66 @@ public class StreamFrame extends JFrame { } private void hideCursor() { - // Transparent 16 x 16 pixel cursor image. - BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); - - // Create a new blank cursor. - Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor( - cursorImg, new Point(0, 0), "blank cursor"); + if (noCursor == null) { + // Transparent 16 x 16 pixel cursor image. + BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); + // Create a new blank cursor. + noCursor = Toolkit.getDefaultToolkit().createCustomCursor( + cursorImg, new Point(0, 0), "blank cursor"); + } // Set the blank cursor to the JFrame. - this.getContentPane().setCursor(blankCursor); - + this.setCursor(noCursor); + this.getContentPane().setCursor(noCursor); + } - + + private void showCursor() { + this.setCursor(Cursor.getDefaultCursor()); + this.getContentPane().setCursor(Cursor.getDefaultCursor()); + } + public void showSpinner(Stage stage) { - + if (spinner == null) { Container c = this.getContentPane(); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); - + spinner = new JProgressBar(); spinner.setIndeterminate(true); spinner.setMaximumSize(new Dimension(150, 30)); - + spinnerLabel = new JLabel(); spinnerLabel.setForeground(Color.white); - + Box spinBox = Box.createHorizontalBox(); spinBox.add(Box.createHorizontalGlue()); spinBox.add(spinner); spinBox.add(Box.createHorizontalGlue()); - + Box lblBox = Box.createHorizontalBox(); lblBox.add(Box.createHorizontalGlue()); lblBox.add(spinnerLabel); lblBox.add(Box.createHorizontalGlue()); - + panel.add(Box.createVerticalGlue()); panel.add(spinBox); panel.add(Box.createVerticalStrut(10)); panel.add(lblBox); panel.add(Box.createVerticalGlue()); - + c.setLayout(new BorderLayout()); c.add(panel, "Center"); } spinnerLabel.setText("Starting " + stage.getName() + "..."); } - + public void hideSpinner() { spinner.setVisible(false); spinnerLabel.setVisible(false); } - + public void close() { dispose(); conn.stop(); diff --git a/limelight-pc/src/com/limelight/input/KeyboardHandler.java b/limelight-pc/src/com/limelight/input/KeyboardHandler.java index e5c9cab..adc82cf 100644 --- a/limelight-pc/src/com/limelight/input/KeyboardHandler.java +++ b/limelight-pc/src/com/limelight/input/KeyboardHandler.java @@ -8,21 +8,21 @@ import com.limelight.nvstream.NvConnection; import com.limelight.nvstream.input.KeyboardPacket; public class KeyboardHandler implements KeyListener { - + private static KeyboardTranslator translator; private StreamFrame parent; - + public KeyboardHandler(NvConnection conn, StreamFrame parent) { translator = new KeyboardTranslator(conn); this.parent = parent; } - + @Override public void keyPressed(KeyEvent event) { short keyMap = translator.translate(event.getKeyCode()); - + byte modifier = 0x0; - + int modifiers = event.getModifiersEx(); if ((modifiers & KeyEvent.SHIFT_DOWN_MASK) != 0) { modifier |= KeyboardPacket.MODIFIER_SHIFT; @@ -35,23 +35,37 @@ public class KeyboardHandler implements KeyListener { } if ((modifiers & KeyEvent.SHIFT_DOWN_MASK) != 0 && - (modifiers & KeyEvent.ALT_DOWN_MASK) != 0 && - (modifiers & KeyEvent.CTRL_DOWN_MASK) != 0 && - event.getKeyCode() == KeyEvent.VK_Q) { + (modifiers & KeyEvent.ALT_DOWN_MASK) != 0 && + (modifiers & KeyEvent.CTRL_DOWN_MASK) != 0 && + event.getKeyCode() == KeyEvent.VK_Q) { System.out.println("quitting"); parent.close(); + } else if ( + (modifiers & KeyEvent.SHIFT_DOWN_MASK) != 0 && + (modifiers & KeyEvent.ALT_DOWN_MASK) != 0 && + (modifiers & KeyEvent.CTRL_DOWN_MASK) != 0) { + parent.freeMouse(); } - + + + translator.sendKeyDown(keyMap, modifier); } @Override public void keyReleased(KeyEvent event) { - short keyMap = translator.translate(event.getKeyCode()); - - byte modifier = 0x0; - int modifiers = event.getModifiersEx(); + + if ((modifiers & KeyEvent.SHIFT_DOWN_MASK) != 0 || + (modifiers & KeyEvent.ALT_DOWN_MASK) != 0 || + (modifiers & KeyEvent.CTRL_DOWN_MASK) != 0) { + parent.captureMouse(); + } + + short keyMap = translator.translate(event.getKeyCode()); + + byte modifier = 0x0; + if ((modifiers & KeyEvent.SHIFT_DOWN_MASK) != 0) { modifier |= KeyboardPacket.MODIFIER_SHIFT; } @@ -61,7 +75,7 @@ public class KeyboardHandler implements KeyListener { if ((modifiers & KeyEvent.ALT_DOWN_MASK) != 0) { modifier |= KeyboardPacket.MODIFIER_ALT; } - + translator.sendKeyUp(keyMap, modifier); } diff --git a/limelight-pc/src/com/limelight/input/MouseHandler.java b/limelight-pc/src/com/limelight/input/MouseHandler.java index 43584b4..87a6f6c 100644 --- a/limelight-pc/src/com/limelight/input/MouseHandler.java +++ b/limelight-pc/src/com/limelight/input/MouseHandler.java @@ -20,6 +20,7 @@ public class MouseHandler implements MouseListener, MouseMotionListener { private JFrame parent; private int lastX = 0; private int lastY = 0; + private boolean captureMouse = true; public MouseHandler(NvConnection conn, JFrame parent) { this.conn = conn; @@ -33,6 +34,16 @@ public class MouseHandler implements MouseListener, MouseMotionListener { } + public void free() { + captureMouse = false; + } + + public void capture() { + moveMouse((int)parent.getLocationOnScreen().getX() + (size.width/2), + (int)parent.getLocationOnScreen().getY() + (size.height/2)); + captureMouse = true; + } + @Override public void mouseClicked(MouseEvent e) { } @@ -43,82 +54,91 @@ public class MouseHandler implements MouseListener, MouseMotionListener { @Override public void mouseExited(MouseEvent e) { - parent.getSize(size); - moveMouse((int)parent.getLocation().getX() + (size.width/2), - (int)parent.getLocation().getY() + (size.height/2)); + if (captureMouse) { + parent.getSize(size); + moveMouse((int)parent.getLocationOnScreen().getX() + (size.width/2), + (int)parent.getLocationOnScreen().getY() + (size.height/2)); + } } @Override public void mousePressed(MouseEvent e) { - byte mouseButton = 0x0; - - if (SwingUtilities.isLeftMouseButton(e)) { - mouseButton = MouseButtonPacket.BUTTON_1; - } - - if (SwingUtilities.isMiddleMouseButton(e)) { - mouseButton = MouseButtonPacket.BUTTON_2; - } - - if (SwingUtilities.isRightMouseButton(e)) { - mouseButton = MouseButtonPacket.BUTTON_3; - } - - if (mouseButton > 0) { - conn.sendMouseButtonDown(mouseButton); + if (captureMouse) { + byte mouseButton = 0x0; + + if (SwingUtilities.isLeftMouseButton(e)) { + mouseButton = MouseButtonPacket.BUTTON_1; + } + + if (SwingUtilities.isMiddleMouseButton(e)) { + mouseButton = MouseButtonPacket.BUTTON_2; + } + + if (SwingUtilities.isRightMouseButton(e)) { + mouseButton = MouseButtonPacket.BUTTON_3; + } + + if (mouseButton > 0) { + conn.sendMouseButtonDown(mouseButton); + } } } @Override public void mouseReleased(MouseEvent e) { - byte mouseButton = 0x0; - - if (SwingUtilities.isLeftMouseButton(e)) { - mouseButton = MouseButtonPacket.BUTTON_1; - } - - if (SwingUtilities.isMiddleMouseButton(e)) { - mouseButton = MouseButtonPacket.BUTTON_2; - } - - if (SwingUtilities.isRightMouseButton(e)) { - mouseButton = MouseButtonPacket.BUTTON_3; - } - - if (mouseButton > 0) { - conn.sendMouseButtonUp(mouseButton); + if (captureMouse) { + byte mouseButton = 0x0; + + if (SwingUtilities.isLeftMouseButton(e)) { + mouseButton = MouseButtonPacket.BUTTON_1; + } + + if (SwingUtilities.isMiddleMouseButton(e)) { + mouseButton = MouseButtonPacket.BUTTON_2; + } + + if (SwingUtilities.isRightMouseButton(e)) { + mouseButton = MouseButtonPacket.BUTTON_3; + } + + if (mouseButton > 0) { + conn.sendMouseButtonUp(mouseButton); + } } } @Override public void mouseDragged(MouseEvent e) { - mouseMoved(e); + if (captureMouse) { + mouseMoved(e); + } } @Override public void mouseMoved(MouseEvent e) { - int x = e.getX(); - int y = e.getY(); - conn.sendMouseMove((short)(x - lastX), (short)(y - lastY)); - lastX = x; - lastY = y; - - parent.getSize(size); + if (captureMouse) { + int x = e.getX(); + int y = e.getY(); + conn.sendMouseMove((short)(x - lastX), (short)(y - lastY)); + lastX = x; + lastY = y; - int leftEdge = (int) parent.getLocation().getX(); - int rightEdge = leftEdge + size.width; - int upperEdge = (int) parent.getLocation().getY(); - int lowerEdge = upperEdge + size.height; + parent.getSize(size); - if (x < leftEdge + 100 || x > rightEdge - 100) { - moveMouse((leftEdge+rightEdge)/2, (upperEdge+lowerEdge)/2); + int leftEdge = (int) parent.getLocationOnScreen().getX(); + int rightEdge = leftEdge + size.width; + int upperEdge = (int) parent.getLocationOnScreen().getY(); + int lowerEdge = upperEdge + size.height; + + if (x < leftEdge + 100 || x > rightEdge - 100) { + moveMouse((leftEdge+rightEdge)/2, (upperEdge+lowerEdge)/2); + } + if (y < upperEdge + 100 || y > lowerEdge - 100) { + moveMouse((leftEdge+rightEdge)/2, (upperEdge+lowerEdge)/2); + } } - if (y < upperEdge + 100 || y > lowerEdge - 100) { - moveMouse((leftEdge+rightEdge)/2, (upperEdge+lowerEdge)/2); - } - } - + private void moveMouse(int x, int y) { robot.mouseMove(x, y); lastX = x;