Remove Gamepad from limelight-pc

This commit is contained in:
Iwan Timmer
2014-01-08 21:48:44 +01:00
parent 031d82bd1f
commit 32a64e92fa
21 changed files with 0 additions and 1001 deletions

View File

@@ -5,8 +5,6 @@ import java.io.IOException;
import com.limelight.binding.LibraryHelper;
import com.limelight.binding.PlatformBinding;
import com.limelight.input.EvdevHandler;
import com.limelight.input.gamepad.Gamepad;
import com.limelight.input.gamepad.GamepadListener;
import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.NvConnectionListener;
import com.limelight.nvstream.StreamConfiguration;
@@ -59,8 +57,6 @@ public class Limelight implements NvConnectionListener {
displayError("Input", "Input (" + input + ") could not be found");
}
}
GamepadListener.getInstance().addDeviceListener(new Gamepad(conn));
}
private void pair() {

View File

@@ -1,222 +0,0 @@
package com.limelight.input.gamepad;
import com.limelight.input.Device;
import com.limelight.input.DeviceListener;
import com.limelight.input.gamepad.GamepadMapping.Mapping;
import com.limelight.input.gamepad.SourceComponent.Type;
import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.input.ControllerPacket;
import com.limelight.settings.GamepadSettingsManager;
/**
* Represents a gamepad connected to the system
* @author Diego Waxemberg
*/
public class Gamepad implements DeviceListener {
private short inputMap = 0x0000;
private byte leftTrigger = 0x00;
private byte rightTrigger = 0x00;
private short rightStickX = 0x0000;
private short rightStickY = 0x0000;
private short leftStickX = 0x0000;
private short leftStickY = 0x0000;
private NvConnection conn;
public Gamepad(NvConnection conn) {
this.conn = conn;
}
public Gamepad() {
this(null);
}
public void setConnection(NvConnection conn) {
this.conn = conn;
}
@Override
public void handleButton(Device device, int buttonId, boolean pressed) {
GamepadMapping mapping = GamepadSettingsManager.getSettings();
Mapping mapped = mapping.get(new SourceComponent(Type.BUTTON, buttonId));
if (mapped == null) {
System.out.println("Unmapped button pressed: " + buttonId);
return;
}
if (!mapped.padComp.isAnalog()) {
handleDigitalComponent(mapped, pressed);
} else {
handleAnalogComponent(mapped.padComp, sanitizeValue(mapped, pressed));
}
//printInfo(device, new SourceComponent(Type.BUTTON, buttonId), mapped.padComp, pressed ? 1F : 0F);
}
@Override
public void handleAxis(Device device, int axisId, float newValue, float lastValue) {
GamepadMapping mapping = GamepadSettingsManager.getSettings();
Mapping mapped = mapping.get(new SourceComponent(Type.AXIS, axisId));
if (mapped == null) {
System.out.println("Unmapped axis moved: " + axisId);
return;
}
float value = sanitizeValue(mapped, newValue);
if (mapped.padComp.isAnalog()) {
handleAnalogComponent(mapped.padComp, value);
} else {
handleDigitalComponent(mapped, (value > 0.5));
}
//printInfo(device, new SourceComponent(Type.AXIS, axisId), mapped.padComp, newValue);
}
private float sanitizeValue(Mapping mapped, boolean value) {
if (mapped.invert) {
return value ? 0F : 1F;
} else {
return value ? 1F : 0F;
}
}
private float sanitizeValue(Mapping mapped, float value) {
float retVal = value;
if (mapped.invert) {
retVal = -retVal;
}
if (mapped.trigger) {
retVal = (retVal + 1) / 2;
}
return retVal;
}
private void handleAnalogComponent(GamepadComponent padComp, float value) {
switch (padComp) {
case LS_X:
leftStickX = (short)Math.round(value * 0x7FFF);
break;
case LS_Y:
leftStickY = (short)Math.round(value * 0x7FFF);
break;
case RS_X:
rightStickX = (short)Math.round(value * 0x7FFF);
break;
case RS_Y:
rightStickY = (short)Math.round(value * 0x7FFF);
break;
case LT:
leftTrigger = (byte)Math.round(value * 0xFF);
break;
case RT:
rightTrigger = (byte)Math.round(value * 0xFF);
break;
default:
System.out.println("A mapping error has occured. Ignoring: " + padComp.name());
break;
}
if (conn != null) {
sendControllerPacket();
}
}
private void handleDigitalComponent(Mapping mapped, boolean pressed) {
switch (mapped.padComp) {
case BTN_A:
toggle(ControllerPacket.A_FLAG, pressed);
break;
case BTN_X:
toggle(ControllerPacket.X_FLAG, pressed);
break;
case BTN_Y:
toggle(ControllerPacket.Y_FLAG, pressed);
break;
case BTN_B:
toggle(ControllerPacket.B_FLAG, pressed);
break;
case DPAD_UP:
toggle(ControllerPacket.UP_FLAG, pressed);
break;
case DPAD_DOWN:
toggle(ControllerPacket.DOWN_FLAG, pressed);
break;
case DPAD_LEFT:
toggle(ControllerPacket.LEFT_FLAG, pressed);
break;
case DPAD_RIGHT:
toggle(ControllerPacket.RIGHT_FLAG, pressed);
break;
case LS_THUMB:
toggle(ControllerPacket.LS_CLK_FLAG, pressed);
break;
case RS_THUMB:
toggle(ControllerPacket.RS_CLK_FLAG, pressed);
break;
case LB:
toggle(ControllerPacket.LB_FLAG, pressed);
break;
case RB:
toggle(ControllerPacket.RB_FLAG, pressed);
break;
case BTN_START:
toggle(ControllerPacket.PLAY_FLAG, pressed);
break;
case BTN_BACK:
toggle(ControllerPacket.BACK_FLAG, pressed);
break;
case BTN_SPECIAL:
toggle(ControllerPacket.SPECIAL_BUTTON_FLAG, pressed);
break;
default:
System.out.println("A mapping error has occured. Ignoring: " + mapped.padComp.name());
return;
}
if (conn != null) {
sendControllerPacket();
}
}
/*
* Sends a controller packet to the specified connection containing the current gamepad values
*/
private void sendControllerPacket() {
if (conn != null) {
conn.sendControllerInput(inputMap, leftTrigger, rightTrigger,
leftStickX, leftStickY, rightStickX, rightStickY);
}
}
/*
* Prints out the specified event information for the given gamepad
* used for debugging, normally unused.
*/
@SuppressWarnings("unused")
private void printInfo(Device device, SourceComponent sourceComp, GamepadComponent padComp, float value) {
StringBuilder builder = new StringBuilder();
builder.append(sourceComp.getType().name() + ": ");
builder.append(sourceComp.getId() + " ");
builder.append("mapped to: " + padComp + " ");
builder.append("changed to ");
System.out.println(builder.toString());
}
/*
* Toggles a flag that indicates the specified button was pressed or released
*/
private void toggle(short button, boolean pressed) {
if (pressed) {
inputMap |= button;
} else {
inputMap &= ~button;
}
}
}

View File

@@ -1,45 +0,0 @@
package com.limelight.input.gamepad;
import java.io.Serializable;
import javax.swing.JLabel;
/**
* Enumerator for every gamepad component GFE recognizes
* @author Diego Waxemberg
*/
public enum GamepadComponent implements Serializable {
BTN_A("Button 1 (A)", false), BTN_X("Button 2 (X)", false), BTN_Y("Button 3 (Y)", false), BTN_B("Button 4 (B)", false),
DPAD_UP("D-pad Up", false), DPAD_DOWN("D-pad Down", false), DPAD_LEFT("D-pad Left", false), DPAD_RIGHT("D-pad Right", false),
LS_X("Left Stick X", true), LS_Y("Left Stick Y", true), RS_X("Right Stick X", true), RS_Y("Right Stick Y", true),
LS_THUMB("Left Stick Button", false), RS_THUMB("Right Stick Button", false),
LT("Left Trigger", true), RT("Right Trigger", true), LB("Left Bumper", false), RB("Right Bumper", false),
BTN_START("Start Button", false), BTN_BACK("Back Button", false), BTN_SPECIAL("Special Button", false);
private JLabel label;
private boolean analog;
/*
* Constructs the enumerator with the given name for a label and whether it is analog or not
*/
private GamepadComponent(String name, boolean analog) {
this.label = new JLabel(name);
this.analog = analog;
}
/**
* Gets the label for this gamepad component
* @return a label with the name of this component as the text
*/
public JLabel getLabel() {
return label;
}
/**
* Checks if this component is analog or digital
* @return whether this component is analog
*/
public boolean isAnalog() {
return analog;
}
}

View File

@@ -1,83 +0,0 @@
package com.limelight.input.gamepad;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import com.limelight.input.Device;
import com.limelight.input.DeviceListener;
/**
* Listens to <code>Controller</code>s connected to this computer and gives any gamepad to the gamepad handler
* @author Diego Waxemberg
*/
public class GamepadListener implements NativeGamepadListener {
private HashMap<Integer, Device> devices;
private List<DeviceListener> listeners;
private static GamepadListener singleton;
public static GamepadListener getInstance() {
if (singleton == null) {
singleton = new GamepadListener();
}
return singleton;
}
private GamepadListener() {
devices = new HashMap<Integer, Device>();
listeners = new LinkedList<DeviceListener>();
}
public int deviceCount() {
return devices.size();
}
public void addDeviceListener(DeviceListener listener) {
listeners.add(listener);
}
public List<DeviceListener> getListeners() {
return Collections.unmodifiableList(listeners);
}
public void removeListener(DeviceListener listener) {
listeners.remove(listener);
}
@Override
public void deviceAttached(int deviceId, int numButtons, int numAxes) {
devices.put(deviceId, new Device(deviceId, numButtons, numAxes));
}
@Override
public void deviceRemoved(int deviceId) {
devices.remove(deviceId);
}
@Override
public void buttonDown(int deviceId, int buttonId) {
Device dev = devices.get(deviceId);
for (DeviceListener listener : listeners) {
listener.handleButton(dev, buttonId, true);
}
}
@Override
public void buttonUp(int deviceId, int buttonId) {
Device dev = devices.get(deviceId);
for (DeviceListener listener : listeners) {
listener.handleButton(dev, buttonId, false);
}
}
@Override
public void axisMoved(int deviceId, int axisId, float value, float lastValue) {
Device dev = devices.get(deviceId);
for (DeviceListener listener : listeners) {
listener.handleAxis(dev, axisId, value, lastValue);
}
}
}

View File

@@ -1,114 +0,0 @@
package com.limelight.input.gamepad;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map.Entry;
/**
* Mappings for gamepad components
* @author Diego Waxemberg
*/
public class GamepadMapping implements Serializable {
private static final long serialVersionUID = -185035113915743149L;
private HashMap<SourceComponent, Mapping> mapping;
/**
* Constructs a new mapping that has nothing mapped.
*/
public GamepadMapping() {
mapping = new HashMap<SourceComponent, Mapping>();
}
/**
* Inserts the specified mapping into this map
* @param toMap a <code>Mapping</code> that will be mapped to the specified gamepad component
* @param comp the gamepad component to map to.
*/
public void insertMapping(Mapping toMap, SourceComponent comp) {
mapping.put(comp, toMap);
}
/**
* Gets the mapping for the specified gamepad component
* @param comp the gamepad component to get a mapping for
* @return a mapping for the requested component
*/
public Mapping get(SourceComponent comp) {
return mapping.get(comp);
}
/**
* Removes the mapping to the specified component
* @param comp the component to no longer be mapped.
*/
public void remove(SourceComponent comp) {
mapping.remove(comp);
}
/**
* Gets the mapped ControllerComponent for the specified ControllerComponent.</br>
* <b>NOTE: Iterates a hashmap, use sparingly</b>
* @param padComp the component to get a mapping for
* @return a mapping or an null if there is none
*/
public Mapping get(GamepadComponent padComp) {
//#allTheJank
for (Entry<SourceComponent, Mapping> entry : mapping.entrySet()) {
if (entry.getValue().padComp == padComp) {
return entry.getValue();
}
}
return null;
}
/**
* Gets the mapping for the specified component.</br>
* <b>NOTE: Iterates a hashmap, use sparingly</b>
* @param padComp the component to get a mapping for
* @return a mapping or an empty string if there is none
*/
public SourceComponent getMapping(GamepadComponent padComp) {
for (Entry<SourceComponent, Mapping> entry : mapping.entrySet()) {
if (entry.getValue().padComp == padComp) {
return entry.getKey();
}
}
return null;
}
/**
* Represents a mapping, that is which gamepad component, whether it is inverted, a trigger, etc.
* @author Diego Waxemberg
*/
public class Mapping implements Serializable {
private static final long serialVersionUID = -8407172977953214242L;
/**
* The component this mapping belongs to
*/
public GamepadComponent padComp;
/**
* Whether the value of this component should be inverted
*/
public boolean invert;
/**
* Whether this component should be treated as a trigger
*/
public boolean trigger;
/**
* Constructs a new mapping with the specified configuration
* @param padComp the component this mapping belongs to
* @param invert whether the value should be inverted
* @param trigger whether this component should be treated as a trigger
*/
public Mapping(GamepadComponent padComp, boolean invert, boolean trigger) {
this.padComp = padComp;
this.invert = invert;
this.trigger = trigger;
}
}
}

View File

@@ -1,182 +0,0 @@
package com.limelight.input.gamepad;
import java.util.ArrayList;
public class NativeGamepad {
public static final int DEFAULT_DEVICE_POLLING_INTERVAL = 1000;
public static final int DEFAULT_EVENT_POLLING_INTERVAL = 20;
private static ArrayList<NativeGamepadListener> listenerList =
new ArrayList<NativeGamepadListener>();
private static boolean running = false;
private static boolean initialized = false;
private static Thread deviceThread = null;
private static Thread eventThread = null;
private static int devicePollingIntervalMs = DEFAULT_DEVICE_POLLING_INTERVAL;
private static int eventPollingIntervalMs = DEFAULT_EVENT_POLLING_INTERVAL;
static {
System.loadLibrary("gamepad_jni");
}
private static native void init();
private static native void shutdown();
private static native int numDevices();
private static native void detectDevices();
private static native void processEvents();
public static void addListener(NativeGamepadListener listener) {
listenerList.add(listener);
}
public static void removeListener(NativeGamepadListener listener) {
listenerList.remove(listener);
}
public static boolean isRunning() {
return running;
}
public static void setDevicePollingInterval(int interval) {
devicePollingIntervalMs = interval;
}
public static int getDevicePollingInterval() {
return devicePollingIntervalMs;
}
public static void setEventPollingInterval(int interval) {
eventPollingIntervalMs = interval;
}
public static int getEventPollingInterval() {
return eventPollingIntervalMs;
}
public static void start() {
if (!initialized) {
NativeGamepad.init();
initialized = true;
}
if (!running) {
startDevicePolling();
startEventPolling();
running = true;
}
}
public static void stop() {
if (running) {
stopEventPolling();
stopDevicePolling();
running = false;
}
}
public static void release() {
if (running) {
throw new IllegalStateException("Cannot release running NativeGamepad");
}
if (initialized) {
NativeGamepad.shutdown();
initialized = false;
}
}
public static int getDeviceCount() {
if (!running) {
throw new IllegalStateException("NativeGamepad not running");
}
return NativeGamepad.numDevices();
}
private static void startDevicePolling() {
deviceThread = new Thread() {
@Override
public void run() {
while (!isInterrupted()) {
NativeGamepad.detectDevices();
try {
Thread.sleep(devicePollingIntervalMs);
} catch (InterruptedException e) {
return;
}
}
}
};
deviceThread.setName("Native Gamepad - Device Polling Thread");
deviceThread.start();
}
private static void startEventPolling() {
eventThread = new Thread() {
@Override
public void run() {
while (!isInterrupted()) {
NativeGamepad.processEvents();
try {
Thread.sleep(eventPollingIntervalMs);
} catch (InterruptedException e) {
return;
}
}
}
};
eventThread.setName("Native Gamepad - Event Polling Thread");
eventThread.start();
}
private static void stopDevicePolling() {
if (deviceThread != null) {
deviceThread.interrupt();
try {
deviceThread.join();
} catch (InterruptedException e) {}
}
}
private static void stopEventPolling() {
if (eventThread != null) {
eventThread.interrupt();
try {
eventThread.join();
} catch (InterruptedException e) {}
}
}
public static void deviceAttachCallback(int deviceId, int numButtons, int numAxes) {
for (NativeGamepadListener listener : listenerList) {
listener.deviceAttached(deviceId, numButtons, numAxes);
}
}
public static void deviceRemoveCallback(int deviceId) {
for (NativeGamepadListener listener : listenerList) {
listener.deviceRemoved(deviceId);
}
}
public static void buttonUpCallback(int deviceId, int buttonId) {
for (NativeGamepadListener listener : listenerList) {
listener.buttonUp(deviceId, buttonId);
}
}
public static void buttonDownCallback(int deviceId, int buttonId) {
for (NativeGamepadListener listener : listenerList) {
listener.buttonDown(deviceId, buttonId);
}
}
public static void axisMovedCallback(int deviceId, int axisId, float value, float lastValue) {
for (NativeGamepadListener listener : listenerList) {
listener.axisMoved(deviceId, axisId, value, lastValue);
}
}
}

View File

@@ -1,13 +0,0 @@
package com.limelight.input.gamepad;
public interface NativeGamepadListener {
public void deviceAttached(int deviceId, int numButtons, int numAxes);
public void deviceRemoved(int deviceId);
public void buttonDown(int deviceId, int buttonId);
public void buttonUp(int deviceId, int buttonId);
public void axisMoved(int deviceId, int axisId, float value, float lastValue);
}

View File

@@ -1,46 +0,0 @@
package com.limelight.input.gamepad;
import java.io.Serializable;
public class SourceComponent implements Serializable {
private static final long serialVersionUID = 2366409458045238273L;
public enum Type { AXIS, BUTTON }
private Type type;
private int id;
public SourceComponent(Type type, int id) {
this.type = type;
this.id = id;
}
public Type getType() {
return type;
}
public int getId() {
return id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || !(obj instanceof SourceComponent)) {
return false;
}
SourceComponent other = (SourceComponent) obj;
return id == other.id && type == other.type;
}
}

View File

@@ -1,46 +0,0 @@
package com.limelight.settings;
import java.io.File;
import com.limelight.input.gamepad.GamepadMapping;
/**
* Manages the gamepad settings
* @author Diego Waxemberg
*/
public abstract class GamepadSettingsManager {
private static GamepadMapping cachedSettings;
/**
* Reads the gamepad settings from the gamepad file and caches the configuration
* @return the gamepad settings
*/
public static GamepadMapping getSettings() {
if (cachedSettings == null) {
System.out.println("Reading Gamepad Settings");
File gamepadFile = SettingsManager.getInstance().getGamepadFile();
GamepadMapping savedMapping = (GamepadMapping)SettingsManager.readSettings(gamepadFile);
cachedSettings = savedMapping;
}
if (cachedSettings == null) {
System.out.println("Unable to get gamepad settings. Using an empty mapping instead.");
cachedSettings = new GamepadMapping();
writeSettings(cachedSettings);
}
return cachedSettings;
}
/**
* Writes the specified mapping to the gamepad file and updates the cached settings
* @param settings the new gamepad mapping to be written out
*/
public static void writeSettings(GamepadMapping settings) {
cachedSettings = settings;
System.out.println("Writing Gamepad Settings");
File gamepadFile = SettingsManager.getInstance().getGamepadFile();
SettingsManager.writeSettings(gamepadFile, settings);
}
}