move everything out of subdirectory

This commit is contained in:
Aaron Neyer
2013-12-15 14:12:57 -05:00
parent 0d8fe8c197
commit 8ed0d6d161
198 changed files with 191 additions and 2359 deletions

View File

@@ -0,0 +1,128 @@
package com.limelight.input;
import com.limelight.nvstream.NvConnection;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.Event;
import net.java.games.input.EventQueue;
public abstract class Gamepad {
protected Controller pad;
private NvConnection conn;
protected short inputMap = 0x0000;
protected byte leftTrigger = 0x00;
protected byte rightTrigger = 0x00;
protected short rightStickX = 0x0000;
protected short rightStickY = 0x0000;
protected short leftStickX = 0x0000;
protected short leftStickY = 0x0000;
public enum ControllerType { XBOX, PS3 };
public static Gamepad createInstance(NvConnection conn, Controller pad, ControllerType type) {
switch (type) {
case XBOX:
return new XBox360Controller(conn, pad);
case PS3:
return new PS3Controller(conn, pad);
default:
return new XBox360Controller(conn, pad);
}
}
public Gamepad(NvConnection conn, Controller pad) {
this.conn = conn;
this.pad = pad;
for (Component comp : pad.getComponents()) {
initValue(comp);
}
}
private void initValue(Component comp) {
handleComponent(comp, comp.getPollData());
}
public boolean poll() {
return pad.poll();
}
public void sendControllerPacket() {
conn.sendControllerInput(inputMap, leftTrigger, rightTrigger,
leftStickX, leftStickY, rightStickX, rightStickY);
}
public void handleEvents() {
EventQueue queue = pad.getEventQueue();
Event event = new Event();
while(queue.getNextEvent(event)) {
/* uncommented for debugging */
//printInfo(pad, event);
handleEvent(event);
sendControllerPacket();
}
}
/*
* used for debugging, normally unused.
*/
@SuppressWarnings("unused")
private void printInfo(Controller gamepad, Event event) {
Component comp = event.getComponent();
StringBuilder builder = new StringBuilder(gamepad.getName());
builder.append(" at ");
builder.append(event.getNanos()).append(": ");
builder.append(comp.getName()).append(" changed to ");
float value = event.getValue();
if(comp.isAnalog()) {
builder.append(value);
} else {
if(value==1.0f) {
builder.append("On");
} else {
builder.append("Off");
}
}
System.out.println(builder.toString());
}
private void handleEvent(Event event) {
Component comp = event.getComponent();
float value = event.getValue();
handleComponent(comp, value);
}
private void handleComponent(Component comp, float value) {
if (comp.isAnalog()) {
handleAnalog(comp, value);
} else {
handleButtons(comp, value);
}
}
protected void toggle(short button, boolean press) {
if (press) {
inputMap |= button;
} else {
inputMap &= ~button;
}
}
protected abstract void handleAnalog(Component comp, float value);
protected abstract void handleButtons(Component comp, float value);
}

View File

@@ -0,0 +1,59 @@
package com.limelight.input;
import java.util.LinkedList;
import java.util.List;
import com.limelight.input.Gamepad.ControllerType;
import com.limelight.nvstream.NvConnection;
import net.java.games.input.Controller;
public class GamepadHandler {
private static LinkedList<Gamepad> gamepads = new LinkedList<Gamepad>();
private static GamepadHandler singleton;
public static void addGamepads(List<Controller> pads, NvConnection conn) {
if (singleton == null) {
singleton = new GamepadHandler();
singleton.startUp();
}
gamepads.clear();
for (Controller pad : pads) {
gamepads.add(Gamepad.createInstance(conn, pad, getType(pad)));
}
}
private static ControllerType getType(Controller pad) {
if (pad.getType() == Controller.Type.GAMEPAD) {
return ControllerType.XBOX;
}
if (pad.getName().contains("PLAYSTATION")) {
return ControllerType.PS3;
}
return null;
}
private void startUp() {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
for (Gamepad gamepad : gamepads) {
if (!gamepad.poll()) {
break;
}
gamepad.handleEvents();
}
try {
Thread.sleep(20);
} catch (InterruptedException e) {}
}
}
}).start();
}
}

View File

@@ -0,0 +1,87 @@
package com.limelight.input;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import com.limelight.gui.StreamFrame;
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;
}
if ((modifiers & KeyEvent.CTRL_DOWN_MASK) != 0) {
modifier |= KeyboardPacket.MODIFIER_CTRL;
}
if ((modifiers & KeyEvent.ALT_DOWN_MASK) != 0) {
modifier |= KeyboardPacket.MODIFIER_ALT;
}
if ((modifiers & KeyEvent.SHIFT_DOWN_MASK) != 0 &&
(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();
return;
}
translator.sendKeyDown(keyMap, modifier);
}
@Override
public void keyReleased(KeyEvent event) {
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;
}
if ((modifiers & KeyEvent.CTRL_DOWN_MASK) != 0) {
modifier |= KeyboardPacket.MODIFIER_CTRL;
}
if ((modifiers & KeyEvent.ALT_DOWN_MASK) != 0) {
modifier |= KeyboardPacket.MODIFIER_ALT;
}
translator.sendKeyUp(keyMap, modifier);
}
@Override
public void keyTyped(KeyEvent event) {
}
}

View File

@@ -0,0 +1,36 @@
package com.limelight.input;
import java.awt.event.KeyEvent;
import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.input.KeycodeTranslator;
public class KeyboardTranslator extends KeycodeTranslator {
public static final short KEY_PREFIX = (short) 0x80;
public KeyboardTranslator(NvConnection conn) {
super(conn);
}
@Override
public short translate(int keycode) {
// change newline to carriage return
if (keycode == KeyEvent.VK_ENTER) {
keycode = 0x0d;
}
// period maps to delete by default so we remap it
if (keycode == KeyEvent.VK_PERIOD) {
keycode = 0xbe;
}
// Nvidia maps period to delete
if (keycode == KeyEvent.VK_DELETE) {
keycode = KeyEvent.VK_PERIOD;
}
return (short) ((KEY_PREFIX << 8) | keycode);
}
}

View File

@@ -0,0 +1,171 @@
package com.limelight.input;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.SwingUtilities;
import com.limelight.gui.StreamFrame;
import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.input.MouseButtonPacket;
public class MouseHandler implements MouseListener, MouseMotionListener {
private NvConnection conn;
private Robot robot;
private Dimension size;
private StreamFrame parent;
private int lastX = 0;
private int lastY = 0;
private boolean captureMouse = true;
private final double mouseThresh = 0.45;
public MouseHandler(NvConnection conn, StreamFrame parent) {
this.conn = conn;
this.parent = parent;
try {
this.robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
size = new Dimension();
}
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) {
if (captureMouse) {
parent.hideCursor();
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
if (captureMouse) {
checkBoundaries(e);
}
}
@Override
public void mousePressed(MouseEvent e) {
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) {
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) {
if (captureMouse) {
mouseMoved(e);
}
}
@Override
public void mouseMoved(MouseEvent e) {
if (captureMouse) {
Point mouse = e.getLocationOnScreen();
int x = (int)mouse.getX();
int y = (int)mouse.getY();
conn.sendMouseMove((short)(x - lastX), (short)(y - lastY));
lastX = x;
lastY = y;
checkBoundaries(e);
}
}
private void checkBoundaries(MouseEvent e) {
parent.getSize(size);
int leftEdge = (int) parent.getLocationOnScreen().getX();
int rightEdge = leftEdge + size.width;
int upperEdge = (int) parent.getLocationOnScreen().getY();
int lowerEdge = upperEdge + size.height;
Point mouse = e.getLocationOnScreen();
double xThresh = (size.width * mouseThresh);
double yThresh = (size.height * mouseThresh);
int newX = (int)mouse.getX();
int newY = (int)mouse.getY();
boolean shouldMoveMouse = false;
if (mouse.getX() < leftEdge + xThresh || mouse.getX() > rightEdge - xThresh) {
newX = (leftEdge + rightEdge) / 2;
shouldMoveMouse = true;
}
if (mouse.getY() < upperEdge + yThresh || mouse.getY() > lowerEdge - yThresh) {
newY = (upperEdge + lowerEdge) / 2;
shouldMoveMouse = true;
}
if (shouldMoveMouse) {
moveMouse(newX, newY);
}
}
private void moveMouse(int x, int y) {
robot.mouseMove(x, y);
lastX = x;
lastY = y;
}
}

View File

@@ -0,0 +1,71 @@
package com.limelight.input;
import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.input.ControllerPacket;
import net.java.games.input.Component;
import net.java.games.input.Controller;
public class PS3Controller extends Gamepad {
public PS3Controller(NvConnection conn, Controller pad) {
super(conn, pad);
}
@Override
protected void handleAnalog(Component comp, float value) {
Component.Identifier id = comp.getIdentifier();
if (id == Component.Identifier.Axis.Z) {
rightStickX = (short)Math.round(value * 0x7FFF);
} else if (id == Component.Identifier.Axis.RZ) {
rightStickY = (short)Math.round(-value * 0x7FFF);
} else if (id == Component.Identifier.Axis.X) {
leftStickX = (short)Math.round(value * 0x7FFF);
} else if (id == Component.Identifier.Axis.Y) {
leftStickY = (short)Math.round(-value * 0x7FFF);
}
}
@Override
protected void handleButtons(Component comp, float value) {
Component.Identifier id = comp.getIdentifier();
boolean press = value > 0.5F;
if (id == Component.Identifier.Button._7) {
toggle(ControllerPacket.LEFT_FLAG, press);
} else if (id == Component.Identifier.Button._5) {
toggle(ControllerPacket.RIGHT_FLAG, press);
} else if (id == Component.Identifier.Button._4) {
toggle(ControllerPacket.UP_FLAG, press);
} else if (id == Component.Identifier.Button._6) {
toggle(ControllerPacket.DOWN_FLAG, press);
} else if (id == Component.Identifier.Button._14) {
toggle(ControllerPacket.A_FLAG, press);
} else if (id == Component.Identifier.Button._15) {
toggle(ControllerPacket.X_FLAG, press);
} else if (id == Component.Identifier.Button._12) {
toggle(ControllerPacket.Y_FLAG, press);
} else if (id == Component.Identifier.Button._13) {
toggle(ControllerPacket.B_FLAG, press);
} else if (id == Component.Identifier.Button._0) {
toggle(ControllerPacket.BACK_FLAG, press);
} else if (id == Component.Identifier.Button._3) {
toggle(ControllerPacket.PLAY_FLAG, press);
} else if (id == Component.Identifier.Button._2) {
toggle(ControllerPacket.RS_CLK_FLAG, press);
} else if (id == Component.Identifier.Button._1) {
toggle(ControllerPacket.LS_CLK_FLAG, press);
} else if (id == Component.Identifier.Button._10) {
toggle(ControllerPacket.LB_FLAG, press);
} else if (id == Component.Identifier.Button._11) {
toggle(ControllerPacket.RB_FLAG, press);
} else if (id == Component.Identifier.Button._16) {
toggle(ControllerPacket.SPECIAL_BUTTON_FLAG, press);
} else if (id == Component.Identifier.Button._8) {
leftTrigger = (byte)Math.round((press ? 1 : 0) * 0xFF);
} else if (id == Component.Identifier.Button._9) {
rightTrigger = (byte)Math.round((press ? 1 : 0) * 0xFF);
}
}
}

View File

@@ -0,0 +1,73 @@
package com.limelight.input;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.input.ControllerPacket;
public class XBox360Controller extends Gamepad {
public XBox360Controller(NvConnection conn, Controller pad) {
super(conn, pad);
}
@Override
protected void handleButtons(Component comp, float value) {
Component.Identifier id = comp.getIdentifier();
boolean press = value > 0.5F;
if (id == Component.Identifier.Button._13) {
toggle(ControllerPacket.LEFT_FLAG, press);
} else if (id == Component.Identifier.Button._14) {
toggle(ControllerPacket.RIGHT_FLAG, press);
} else if (id == Component.Identifier.Button._11) {
toggle(ControllerPacket.UP_FLAG, press);
} else if (id == Component.Identifier.Button._12) {
toggle(ControllerPacket.DOWN_FLAG, press);
} else if (id == Component.Identifier.Button._0) {
toggle(ControllerPacket.A_FLAG, press);
} else if (id == Component.Identifier.Button._2) {
toggle(ControllerPacket.X_FLAG, press);
} else if (id == Component.Identifier.Button._3) {
toggle(ControllerPacket.Y_FLAG, press);
} else if (id == Component.Identifier.Button._1) {
toggle(ControllerPacket.B_FLAG, press);
} else if (id == Component.Identifier.Button._9) {
toggle(ControllerPacket.BACK_FLAG, press);
} else if (id == Component.Identifier.Button._8) {
toggle(ControllerPacket.PLAY_FLAG, press);
} else if (id == Component.Identifier.Button._7) {
toggle(ControllerPacket.RS_CLK_FLAG, press);
} else if (id == Component.Identifier.Button._6) {
toggle(ControllerPacket.LS_CLK_FLAG, press);
} else if (id == Component.Identifier.Button._4) {
toggle(ControllerPacket.LB_FLAG, press);
} else if (id == Component.Identifier.Button._5) {
toggle(ControllerPacket.RB_FLAG, press);
} else if (id == Component.Identifier.Button._10) {
toggle(ControllerPacket.SPECIAL_BUTTON_FLAG, press);
}
}
@Override
protected void handleAnalog(Component comp, float value) {
Component.Identifier id = comp.getIdentifier();
if (id == Component.Identifier.Axis.RX) {
rightStickX = (short)Math.round(value * 0x7FFF);
} else if (id == Component.Identifier.Axis.RY) {
rightStickY = (short)Math.round(-value * 0x7FFF);
} else if (id == Component.Identifier.Axis.X) {
leftStickX = (short)Math.round(value * 0x7FFF);
} else if (id == Component.Identifier.Axis.Y) {
leftStickY = (short)Math.round(-value * 0x7FFF);
} else if (id == Component.Identifier.Axis.Z) {
leftTrigger = (byte)Math.round((value + 1) / 2 * 0xFF);
} else if (id == Component.Identifier.Axis.RZ) {
rightTrigger = (byte)Math.round((value + 1) / 2 * 0xFF);
}
}
}