mirror of
https://github.com/Maschell/HIDtoVPADNetworkClient.git
synced 2024-11-25 15:26:53 +01:00
My approach on improving it, currently not really working
NOTE: I changed the protoctol a bit. The actual network code is not finished and still under developement. BUT: - I simplified the GUI Classes - Added XInput support - And changed too much thing, can't list all here. - Comments etc missing. Just want to have a commited version until I break stuff.
This commit is contained in:
parent
25207c85ca
commit
82a25e0cf4
17
pom.xml
17
pom.xml
@ -16,11 +16,28 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>jitpack.io</id> <!-- JitPack allows github repo to be used as a maven repo -->
|
||||||
|
<url>https://jitpack.io</url> <!-- For documentation: http://jitpack.io/ -->
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hid4java</groupId>
|
<groupId>org.hid4java</groupId>
|
||||||
<artifactId>hid4java</artifactId>
|
<artifactId>hid4java</artifactId>
|
||||||
<version>0.4.0</version>
|
<version>0.4.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.16.12</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.strikerx3</groupId>
|
||||||
|
<artifactId>jxinput</artifactId>
|
||||||
|
<version>1eb4087</version> <!-- JXInput 0.7 -->
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
@ -25,9 +25,8 @@ import javax.swing.SwingUtilities;
|
|||||||
|
|
||||||
import org.hid4java.HidManager;
|
import org.hid4java.HidManager;
|
||||||
|
|
||||||
import net.ash.HIDToVPADNetworkClient.controller.ControllerManager;
|
|
||||||
import net.ash.HIDToVPADNetworkClient.gui.ControllerDetector;
|
|
||||||
import net.ash.HIDToVPADNetworkClient.gui.GuiMain;
|
import net.ash.HIDToVPADNetworkClient.gui.GuiMain;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.manager.ActiveControllerManager;
|
||||||
import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
|
import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
|
||||||
|
|
||||||
/* Ash's todo list
|
/* Ash's todo list
|
||||||
@ -38,10 +37,9 @@ import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Hello World!");
|
System.out.println("Hello World!");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new ControllerManager();
|
new Thread(ActiveControllerManager.getInstance()).start();
|
||||||
new NetworkManager();
|
new Thread(NetworkManager.getInstance()).start();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
fatal();
|
fatal();
|
||||||
@ -52,11 +50,9 @@ public class Main {
|
|||||||
GuiMain.createGUI();
|
GuiMain.createGUI();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
new ControllerDetector().start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void fatal() {
|
public static void fatal() {
|
||||||
System.err.println("HID To VPAD Network Client encountered an irrecoverable error.");
|
System.err.println("HID To VPAD Network Client encountered an irrecoverable error.");
|
||||||
System.err.println("Exiting...");
|
System.err.println("Exiting...");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
|
@ -21,13 +21,76 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package net.ash.HIDToVPADNetworkClient.controller;
|
package net.ash.HIDToVPADNetworkClient.controller;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Synchronized;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.manager.ControllerManager;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.util.Utilities;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main controller interface, extended by controller drivers.
|
* Main controller interface, extended by controller drivers.
|
||||||
* <br><br>
|
* <br><br>
|
||||||
* See {@link LinuxDevInputController} for a full implementation.
|
* See {@link LinuxDevInputController} for a full implementation.
|
||||||
* @author ash
|
* @author ash
|
||||||
*/
|
*/
|
||||||
public interface Controller {
|
public abstract class Controller implements Runnable{
|
||||||
|
private boolean active;
|
||||||
|
@Getter private final ControllerType type;
|
||||||
|
@Getter private final String identifier;
|
||||||
|
private byte[] latestData = null;
|
||||||
|
|
||||||
|
boolean shutdown = false;
|
||||||
|
boolean shutdownDone = false;
|
||||||
|
private Object dataLock = new Object();
|
||||||
|
private Object shutdownLock = new Object();
|
||||||
|
|
||||||
|
public Controller(ControllerType type, String identifier) throws ControllerInitializationFailedException{
|
||||||
|
this.type = type;
|
||||||
|
this.identifier = identifier;
|
||||||
|
if(!initController(identifier)){
|
||||||
|
throw new ControllerInitializationFailedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
boolean shutdownState = shutdown;
|
||||||
|
while(!shutdownState){
|
||||||
|
Utilities.sleep(1000);
|
||||||
|
while(isActive()) {
|
||||||
|
byte[] newData = pollLatestData();
|
||||||
|
if(newData != null){
|
||||||
|
setLatestData(newData);
|
||||||
|
}
|
||||||
|
Utilities.sleep(10);
|
||||||
|
}
|
||||||
|
synchronized (shutdownLock) {
|
||||||
|
shutdownState = shutdown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
synchronized (shutdownLock) {
|
||||||
|
shutdownDone = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized("dataLock")
|
||||||
|
private void setLatestData(byte[] newData) {
|
||||||
|
this.latestData = newData;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized("dataLock")
|
||||||
|
public byte[] getLatestData() {
|
||||||
|
if(latestData != null){
|
||||||
|
byte[] data = this.latestData.clone();
|
||||||
|
this.latestData = null;
|
||||||
|
return data;
|
||||||
|
}else{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract byte[] pollLatestData();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up the driver.
|
* Sets up the driver.
|
||||||
* <br>
|
* <br>
|
||||||
@ -36,64 +99,93 @@ public interface Controller {
|
|||||||
* @param arg Driver-specific init argument, see {@link ControllerManager} and {@link ControllerDetector}.
|
* @param arg Driver-specific init argument, see {@link ControllerManager} and {@link ControllerDetector}.
|
||||||
* @return Whether initialization was successful.
|
* @return Whether initialization was successful.
|
||||||
*/
|
*/
|
||||||
public boolean initController(Object arg);
|
public abstract boolean initController(String identifier);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allocates and returns a copy of the latest data available from the controller.
|
* Destroys the controller driver and ends the polling thread.
|
||||||
*
|
|
||||||
* @return A ControllerData instance containing the latest controller data.
|
|
||||||
*/
|
*/
|
||||||
public ControllerData getLatestData();
|
public void destroyAll(){
|
||||||
|
destroyDriver();
|
||||||
|
endThread();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to tell a driver whether or not to poll the controller.
|
* Destroys the controller driver.
|
||||||
* <br>
|
*/
|
||||||
* Is currently only ever used to initialize a driver (poll=true).
|
public abstract void destroyDriver();
|
||||||
* destroy() is called for deinitialization.
|
|
||||||
* <br><br>
|
|
||||||
* <i>Candidate to be removed during refactoring.</i>
|
|
||||||
*
|
|
||||||
* @param poll Whether or not the driver should poll the controller.
|
|
||||||
*/
|
|
||||||
public void setPollingState(boolean poll);
|
|
||||||
|
|
||||||
/**
|
private void endThread() {
|
||||||
* Destroys the controller driver.
|
new Thread(new Runnable() {
|
||||||
* <br>
|
@Override
|
||||||
* Will not return until all threads are stopped and resources are freed.
|
public void run() {
|
||||||
*/
|
setActive(false);
|
||||||
public void destroy();
|
|
||||||
|
synchronized (shutdownLock) {
|
||||||
/**
|
shutdown = true;
|
||||||
* Sets the deviceSlot and padSlot to be returned by {@link #getDeviceSlot() getDeviceSlot} and {@link #getPadSlot() getPadSlot}.
|
}
|
||||||
* @param deviceSlot Value to be returned by {@link #getDeviceSlot() getDeviceSlot}
|
|
||||||
* @param padSlot Value to be returned by {@link #getPadSlot() getPadSlot}
|
boolean done = false;
|
||||||
*/
|
int i = 0;
|
||||||
public void setSlotData(short deviceSlot, byte padSlot);
|
while(!done){
|
||||||
|
synchronized (shutdownLock) {
|
||||||
/**
|
done = shutdownDone;
|
||||||
* Gets the previously set device slot (see {@link #setSlotData(short, byte) setSlotData})
|
}
|
||||||
* @return The controller's device slot.
|
Utilities.sleep(50);
|
||||||
*/
|
if(i++ > 50) System.out.println("Thread doesn't stop!!");
|
||||||
public short getDeviceSlot();
|
}
|
||||||
|
}
|
||||||
/**
|
}).start();
|
||||||
* Gets the previously set pad slot (see {@link #setSlotData(short, byte) setSlotData})
|
}
|
||||||
* @return The controller's pad slot.
|
|
||||||
*/
|
public abstract short getVID();
|
||||||
public byte getPadSlot();
|
|
||||||
|
public abstract short getPID();
|
||||||
/**
|
|
||||||
* Returns a unique handle for this controller driver.
|
@Synchronized("shutdownLock")
|
||||||
* <br>
|
public boolean isActive() {
|
||||||
* Please note that this is unique to the <i>driver</i>, not the controller it's connected to.
|
return active;
|
||||||
* @return The driver's handle.
|
}
|
||||||
*/
|
|
||||||
public int getHandle();
|
public void setActive(boolean active) {
|
||||||
|
this.active = active;
|
||||||
/**
|
}
|
||||||
* Gets the controller's ID. This is often identical to the argument to {@link #initController(Object) initController}.
|
|
||||||
* @return The controller's ID.
|
@Override
|
||||||
*/
|
public String toString(){
|
||||||
public String getID();
|
return getType() + " " + getIdentifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
|
||||||
|
result = prime * result + ((type == null) ? 0 : type.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
Controller other = (Controller) obj;
|
||||||
|
if (identifier == null) {
|
||||||
|
if (other.identifier != null)
|
||||||
|
return false;
|
||||||
|
} else if (!identifier.equals(other.identifier))
|
||||||
|
return false;
|
||||||
|
if (type != other.type)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ControllerType {
|
||||||
|
HID4JAVA, LINUX, XINPUT13,XINPUT14
|
||||||
|
}
|
||||||
|
}
|
@ -1,126 +0,0 @@
|
|||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*******************************************************************************/
|
|
||||||
package net.ash.HIDToVPADNetworkClient.controller;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import net.ash.HIDToVPADNetworkClient.gui.GuiController;
|
|
||||||
import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
|
|
||||||
|
|
||||||
public class ControllerManager {
|
|
||||||
private static ControllerManager instance;
|
|
||||||
|
|
||||||
public ControllerManager() throws Exception {
|
|
||||||
if (instance != null) {
|
|
||||||
throw new Exception("ControllerManager already has an instance!");
|
|
||||||
}
|
|
||||||
instance = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean startConnection(String ip, List<GuiController> controllersIn) {
|
|
||||||
List<Controller> controllers = setupControllers(controllersIn);
|
|
||||||
|
|
||||||
//Boot up all drivers
|
|
||||||
for (Controller c : controllers) {
|
|
||||||
c.setPollingState(true);
|
|
||||||
|
|
||||||
NetworkManager.instance().addController(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
NetworkManager.instance().connect(ip);
|
|
||||||
return NetworkManager.instance().isConnected();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stopConnection() {
|
|
||||||
NetworkManager.instance().removeAllControllers();
|
|
||||||
NetworkManager.instance().disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateControllers(List<GuiController> controllersIn) {
|
|
||||||
HashSet<Integer> m = new HashSet<Integer>();
|
|
||||||
for (GuiController g : controllersIn) {
|
|
||||||
if (!g.getActiveState()) continue;
|
|
||||||
m.add(g.getId().hashCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMap<Integer, Controller> d = NetworkManager.instance().getControllers();
|
|
||||||
for (Controller c : d.values()) {
|
|
||||||
if (!m.contains(c.getID().hashCode())) {
|
|
||||||
NetworkManager.instance().removeController(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
List<GuiController> list = new ArrayList<GuiController>();
|
|
||||||
for (GuiController g : controllersIn) {
|
|
||||||
if (!d.containsKey(g.getId().hashCode())) {
|
|
||||||
list.add(g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Controller c : setupControllers(list)) {
|
|
||||||
c.setPollingState(true);
|
|
||||||
NetworkManager.instance().addController(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Controller> setupControllers(List<GuiController> controllers) {
|
|
||||||
List<Controller> out = new ArrayList<Controller>();
|
|
||||||
|
|
||||||
for (GuiController g : controllers) {
|
|
||||||
if (!g.getActiveState()) continue;
|
|
||||||
|
|
||||||
Controller ctrl;
|
|
||||||
|
|
||||||
switch (g.getType()) {
|
|
||||||
case HID4JAVA:
|
|
||||||
ctrl = new Hid4JavaController();
|
|
||||||
break;
|
|
||||||
case LINUX:
|
|
||||||
ctrl = new LinuxDevInputController();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
System.out.println("[ControllerManager] Unsupported controller type " + g.getType().name());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctrl.initController(g.getId());
|
|
||||||
|
|
||||||
out.add(ctrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void detachController(Controller c) {
|
|
||||||
HashMap<Integer, Controller> m = NetworkManager.instance().getControllers();
|
|
||||||
if (m.containsKey(c.getID().hashCode())) {
|
|
||||||
NetworkManager.instance().removeController(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ControllerManager instance() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
}
|
|
@ -21,91 +21,60 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package net.ash.HIDToVPADNetworkClient.controller;
|
package net.ash.HIDToVPADNetworkClient.controller;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.hid4java.HidDevice;
|
import org.hid4java.HidDevice;
|
||||||
import org.hid4java.HidManager;
|
|
||||||
|
|
||||||
import net.ash.HIDToVPADNetworkClient.util.HandleFoundry;
|
import lombok.AccessLevel;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.util.HID4JavaManager;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.util.Utilities;
|
||||||
|
|
||||||
public class Hid4JavaController implements Controller {
|
public class Hid4JavaController extends Controller {
|
||||||
static final int PACKET_LENGTH = 64;
|
public Hid4JavaController(String identifier) throws ControllerInitializationFailedException {
|
||||||
|
super(ControllerType.HID4JAVA, identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final int PACKET_LENGTH = 128;
|
||||||
|
|
||||||
private String path = null;
|
@Getter @Setter(AccessLevel.PRIVATE)
|
||||||
private HidDevice controller = null;
|
private HidDevice hidDevice;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean initController(Object arg) {
|
public boolean initController(String identifier) {
|
||||||
for (HidDevice device : HidManager.getHidServices().getAttachedHidDevices()) {
|
HidDevice device = HID4JavaManager.getDeviceByPath(identifier);
|
||||||
if (device.getPath().equals(arg.toString())) {
|
//device.setNonBlocking(true); //TODO: What does this do? This is done no in a extra Thread so it shouldn't matter.
|
||||||
controller = device;
|
device.open();
|
||||||
controller.setNonBlocking(true);
|
Utilities.sleep(20); //What a bit until is opened.
|
||||||
|
if (device == null | !device.isOpen()) return false;
|
||||||
path = arg.toString();
|
setHidDevice(device);
|
||||||
|
//System.out.println("ctrl: " + device.isOpen() + " " + device.getLastErrorMessage());
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("ctrl: " + controller.open() + " " + controller.getLastErrorMessage());
|
|
||||||
if (controller == null | !controller.isOpen()) return false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPollingState(boolean poll) {
|
public byte[] pollLatestData() {
|
||||||
|
byte[] data = new byte[PACKET_LENGTH];
|
||||||
}
|
int length = getHidDevice().read(data);
|
||||||
|
if(length <= 0) return null;
|
||||||
|
//if(length > data.length) System.out.println("WTF?");
|
||||||
|
return Arrays.copyOf(data, length);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ControllerData getLatestData() {
|
public void destroyDriver() {
|
||||||
ControllerData data = new ControllerData(getVID(), getPID(), getHandle(), new byte[1200]);
|
getHidDevice().close();
|
||||||
System.out.println("Data size: " + controller.read(data.data, 500));
|
|
||||||
System.out.println("hrm: " + controller.getLastErrorMessage());
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
controller.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private short getVID() {
|
@Override
|
||||||
return controller.getVendorId();
|
public short getVID() {
|
||||||
|
return getHidDevice().getVendorId();
|
||||||
}
|
}
|
||||||
|
|
||||||
private short getPID() {
|
|
||||||
return controller.getProductId();
|
|
||||||
}
|
|
||||||
|
|
||||||
private short deviceSlot = 0;
|
|
||||||
private byte padSlot = 0;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setSlotData(short deviceSlot, byte padSlot) {
|
public short getPID() {
|
||||||
this.deviceSlot = deviceSlot;
|
return getHidDevice().getProductId();
|
||||||
this.padSlot = padSlot;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public short getDeviceSlot() {
|
|
||||||
return deviceSlot;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte getPadSlot() {
|
|
||||||
return padSlot;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int handle = 0;
|
|
||||||
@Override
|
|
||||||
public int getHandle() {
|
|
||||||
if (handle == 0) {
|
|
||||||
handle = HandleFoundry.next();
|
|
||||||
}
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getID() {
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
}
|
|
@ -26,67 +26,65 @@ import java.io.DataInputStream;
|
|||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import net.ash.HIDToVPADNetworkClient.util.HandleFoundry;
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException;
|
||||||
|
|
||||||
public class LinuxDevInputController extends Thread implements Controller {
|
public class LinuxDevInputController extends Controller implements Runnable{
|
||||||
public static final int NUM_SUPPORTED_AXIS = 10; //possibly off-by-one
|
public LinuxDevInputController(String identifier) throws ControllerInitializationFailedException {
|
||||||
|
super(ControllerType.LINUX, identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final int NUM_SUPPORTED_AXIS = 10; //possibly off-by-one
|
||||||
public static final int CONTROLLER_DATA_SIZE = Long.BYTES + (Byte.BYTES * NUM_SUPPORTED_AXIS);
|
public static final int CONTROLLER_DATA_SIZE = Long.BYTES + (Byte.BYTES * NUM_SUPPORTED_AXIS);
|
||||||
|
|
||||||
private static final byte JS_EVENT_BUTTON = 0x01;
|
private static final byte JS_EVENT_BUTTON = 0x01;
|
||||||
private static final byte JS_EVENT_INIT = (byte)0x80;
|
private static final byte JS_EVENT_INIT = (byte)0x80;
|
||||||
private static final byte JS_EVENT_AXIS = 0x02;
|
private static final byte JS_EVENT_AXIS = 0x02;
|
||||||
|
|
||||||
private DataInputStream controller;
|
@Getter @Setter private DataInputStream controller;
|
||||||
private ControllerData controllerData;
|
|
||||||
|
|
||||||
private String id;
|
@Getter @Setter private short VID;
|
||||||
|
@Getter @Setter private short PID;
|
||||||
private short deviceSlot = 0;
|
|
||||||
private byte padSlot = 0;
|
|
||||||
|
|
||||||
private boolean shouldProcessEvents = false;
|
|
||||||
private boolean shutdown = false;
|
|
||||||
|
|
||||||
public LinuxDevInputController() {
|
|
||||||
super("LinuxDevInputController");
|
|
||||||
controllerData = new ControllerData((short)0, (short)0, 0, new byte[CONTROLLER_DATA_SIZE]);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public boolean initController(String identifier) {
|
||||||
for (;;) {
|
try {
|
||||||
for (;;) {
|
controller = new DataInputStream(new BufferedInputStream(new FileInputStream(identifier)));
|
||||||
if (!shouldProcessEvents) break;
|
} catch (Exception e) {
|
||||||
|
System.err.println("[LinuxDevInputController] Couldn't open " + identifier + " as file!");
|
||||||
processNextControllerEvent();
|
e.printStackTrace();
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
if (shutdown) break;
|
|
||||||
/* Not polling. Wait for setPollingState to wake us up. */
|
setVID((short)(identifier.hashCode() & 0xFFFF));
|
||||||
try {
|
setPID((short)((identifier.hashCode() >> Short.BYTES) & 0xFFFF));
|
||||||
this.wait();
|
System.out.println("[LinuxDevInputController] " + identifier.toString() + " fakevid: " + Integer.toHexString((int)getVID() & 0xFFFF) + " fakepid: " + Integer.toHexString((int)getPID() & 0xFFFF));
|
||||||
} catch (InterruptedException e) {}
|
|
||||||
} //for (;;)
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long buttonState = 0;
|
private long buttonState = 0;
|
||||||
private byte[] axisState = new byte[NUM_SUPPORTED_AXIS];
|
private byte[] axisState = new byte[NUM_SUPPORTED_AXIS];
|
||||||
private void processNextControllerEvent() {
|
|
||||||
|
@Override
|
||||||
|
public byte[] pollLatestData() {
|
||||||
|
DataInputStream inputStream = getController();
|
||||||
//Read out next event from controller
|
//Read out next event from controller
|
||||||
/*int time;*/
|
/*int time;*/
|
||||||
short value;
|
short value;
|
||||||
byte type, number;
|
byte type, number;
|
||||||
try {
|
try {
|
||||||
/*time = */controller.readInt();
|
/*time = */inputStream.readInt();
|
||||||
value = controller.readShort();
|
value = inputStream.readShort();
|
||||||
type = controller.readByte();
|
type = inputStream.readByte();
|
||||||
number = controller.readByte();
|
number = inputStream.readByte();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("[LinuxDevInputController] Couldn't read from controller!");
|
System.err.println("[LinuxDevInputController] Couldn't read from controller!");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.out.println("[LinuxDevInputController] Detaching...");
|
System.out.println("[LinuxDevInputController] Detaching...");
|
||||||
ControllerManager.instance().detachController(this);
|
setActive(false);
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Treat init events as normal (clear init bit)
|
//Treat init events as normal (clear init bit)
|
||||||
@ -95,7 +93,7 @@ public class LinuxDevInputController extends Thread implements Controller {
|
|||||||
if (type == JS_EVENT_BUTTON) {
|
if (type == JS_EVENT_BUTTON) {
|
||||||
if (number >= Long.SIZE) {
|
if (number >= Long.SIZE) {
|
||||||
System.out.println("[LinuxDevInputController] Button number " + number + " out of range; ignoring");
|
System.out.println("[LinuxDevInputController] Button number " + number + " out of range; ignoring");
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value != 0) {
|
if (value != 0) {
|
||||||
@ -108,7 +106,7 @@ public class LinuxDevInputController extends Thread implements Controller {
|
|||||||
} else if (type == JS_EVENT_AXIS) {
|
} else if (type == JS_EVENT_AXIS) {
|
||||||
if (number >= NUM_SUPPORTED_AXIS) {
|
if (number >= NUM_SUPPORTED_AXIS) {
|
||||||
System.out.println("[LinuxDevInputController] Axis number " + number + " out of range; ignoring");
|
System.out.println("[LinuxDevInputController] Axis number " + number + " out of range; ignoring");
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
//Do byteswap
|
//Do byteswap
|
||||||
value = (short)(((value & 0xFF) << Byte.SIZE) | ((value & 0xFF00) >> Byte.SIZE));
|
value = (short)(((value & 0xFF) << Byte.SIZE) | ((value & 0xFF00) >> Byte.SIZE));
|
||||||
@ -125,103 +123,20 @@ public class LinuxDevInputController extends Thread implements Controller {
|
|||||||
for (int i = Long.BYTES; i < CONTROLLER_DATA_SIZE; i++) {
|
for (int i = Long.BYTES; i < CONTROLLER_DATA_SIZE; i++) {
|
||||||
newData[i] = axisState[i - Long.BYTES];
|
newData[i] = axisState[i - Long.BYTES];
|
||||||
}
|
}
|
||||||
synchronized (controllerData) {
|
return newData;
|
||||||
controllerData.data = newData;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ControllerData getLatestData() {
|
public void destroyDriver() {
|
||||||
synchronized (controllerData) {
|
|
||||||
return new ControllerData(getVID(), getPID(), getHandle(), controllerData.getData());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean initController(Object arg) {
|
|
||||||
try {
|
try {
|
||||||
controller = new DataInputStream(new BufferedInputStream(new FileInputStream(arg.toString())));
|
controller.close();
|
||||||
} catch (Exception e) {
|
} catch (IOException e) {
|
||||||
System.err.println("[LinuxDevInputController] Couldn't open " + arg.toString() + " as file!");
|
}
|
||||||
e.printStackTrace();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
fakevid = (short)(arg.hashCode() & 0xFFFF);
|
|
||||||
fakepid = (short)((arg.hashCode() >> Short.BYTES) & 0xFFFF);
|
|
||||||
System.out.println("[LinuxDevInputController] " + arg.toString() + " fakevid: " + Integer.toHexString((int)fakevid & 0xFFFF) + " fakepid: " + Integer.toHexString((int)fakepid & 0xFFFF));
|
|
||||||
|
|
||||||
id = arg.toString();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setPollingState(boolean poll) {
|
|
||||||
shouldProcessEvents = poll;
|
|
||||||
if (this.getState() == Thread.State.NEW) {
|
|
||||||
this.start();
|
|
||||||
} else if (this.getState() == Thread.State.WAITING){
|
|
||||||
this.notify();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
shutdown = true;
|
|
||||||
setPollingState(false);
|
|
||||||
|
|
||||||
if (!this.equals(Thread.currentThread())) {
|
|
||||||
while (this.getState() != Thread.State.TERMINATED) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
controller.close();
|
|
||||||
} catch (IOException e) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
private short fakevid;
|
|
||||||
private short getVID() {
|
|
||||||
return fakevid;
|
|
||||||
}
|
|
||||||
|
|
||||||
private short fakepid;
|
|
||||||
private short getPID() {
|
|
||||||
return fakepid;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getID() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int handle = 0;
|
|
||||||
@Override
|
|
||||||
public int getHandle() {
|
|
||||||
if (handle == 0) {
|
|
||||||
handle = HandleFoundry.next();
|
|
||||||
}
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setSlotData(short deviceSlot, byte padSlot) {
|
|
||||||
this.deviceSlot = deviceSlot;
|
|
||||||
this.padSlot = padSlot;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public short getDeviceSlot() {
|
|
||||||
return deviceSlot;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte getPadSlot() {
|
|
||||||
return padSlot;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[" + super.toString() + ";VID," + Integer.toHexString((int)getVID() & 0xFFFF) + ";PID," + Integer.toHexString((int)getPID() & 0xFFFF) + ";run," + shouldProcessEvents + ((controller == null) ? ";uninitialised]" : ";initialised]");
|
return "[" + super.toString() + ";VID," + Integer.toHexString((int)getVID() & 0xFFFF) + ";PID," + Integer.toHexString((int)getPID() & 0xFFFF) + ";run," + isActive() + ((controller == null) ? ";uninitialised]" : ";initialised]");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,187 +0,0 @@
|
|||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*******************************************************************************/
|
|
||||||
//This file is only here so I can remember just how shocking my first attempt
|
|
||||||
//at a driver like this was. It should never, NEVER be used.
|
|
||||||
|
|
||||||
/* I'M SO SORRY
|
|
||||||
* Please do not use this
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.ash.HIDToVPADNetworkClient.controller;
|
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import net.ash.HIDToVPADNetworkClient.util.BlockingIOStabbifier;
|
|
||||||
import net.ash.HIDToVPADNetworkClient.util.WakeupThread;
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public class LinuxDevInputControllerLegacyDriver extends Thread implements Controller {
|
|
||||||
private DataInputStream controller;
|
|
||||||
private String path;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean initController(Object arg) {
|
|
||||||
try {
|
|
||||||
path = (String)arg;
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
System.err.println("LinuxDevInputController recieved bad argument!");
|
|
||||||
e.printStackTrace();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
controller = new DataInputStream(new BlockingIOStabbifier(path));
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
System.err.println("Could not open " + path + "!");
|
|
||||||
e.printStackTrace();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
data = new ControllerData();
|
|
||||||
data.data = new byte[100]; //100 BUTTON MAX
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean shouldPoll = false;
|
|
||||||
WakeupThread wakeup;
|
|
||||||
//@Override
|
|
||||||
public void setPollingState(boolean poll, int pollInterval) {
|
|
||||||
if (poll) {
|
|
||||||
if (this.getState() == Thread.State.NEW) {
|
|
||||||
shouldPoll = poll;
|
|
||||||
this.start();
|
|
||||||
}
|
|
||||||
wakeup = new WakeupThread(LinuxDevInputControllerThreadLock);
|
|
||||||
wakeup.setTimeout(pollInterval);
|
|
||||||
wakeup.start();
|
|
||||||
}
|
|
||||||
shouldPoll = poll;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object LinuxDevInputControllerThreadLock = new Object();
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
for (;;) {
|
|
||||||
while (shouldPoll) {
|
|
||||||
updateControllerData();
|
|
||||||
|
|
||||||
synchronized (LinuxDevInputControllerThreadLock) {
|
|
||||||
try {
|
|
||||||
LinuxDevInputControllerThreadLock.wait();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
wakeup.tryStop();
|
|
||||||
synchronized (LinuxDevInputControllerThreadLock) {
|
|
||||||
try {
|
|
||||||
LinuxDevInputControllerThreadLock.wait();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ControllerData data;
|
|
||||||
|
|
||||||
private void updateControllerData() {
|
|
||||||
try {
|
|
||||||
while (controller.available() > 8) { /* 8 bytes to a joystick packet */
|
|
||||||
/*int time = */controller.readInt();
|
|
||||||
short val = controller.readShort();
|
|
||||||
byte type = controller.readByte();
|
|
||||||
byte num = controller.readByte();
|
|
||||||
num *= 2;
|
|
||||||
if (type == 0x1 && !(num >= 100)) {
|
|
||||||
synchronized (data) {
|
|
||||||
data.data[num] = (byte)(val & 0xFF);
|
|
||||||
data.data[num + 1] = (byte)((val >> 8) & 0xFF);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ControllerData getLatestData() {
|
|
||||||
synchronized (data) {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public short getPID() {
|
|
||||||
return data.getPID();
|
|
||||||
}
|
|
||||||
|
|
||||||
public short getVID() {
|
|
||||||
return data.getVID();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFakePid(short pid) {
|
|
||||||
//this.data.pid = pid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFakeVid(short vid) {
|
|
||||||
//this.data.vid = vid;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setSlotData(short deviceSlot, byte padSlot) {
|
|
||||||
// Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public short getDeviceSlot() {
|
|
||||||
// Auto-generated method stub
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte getPadSlot() {
|
|
||||||
// Auto-generated method stub
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getHandle() {
|
|
||||||
// Auto-generated method stub
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getID() {
|
|
||||||
// Auto-generated method stub
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setPollingState(boolean poll) {
|
|
||||||
// Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -21,28 +21,10 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package net.ash.HIDToVPADNetworkClient.controller;
|
package net.ash.HIDToVPADNetworkClient.controller;
|
||||||
|
|
||||||
public class ControllerData {
|
import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException;
|
||||||
private short vid;
|
|
||||||
private short pid;
|
public class XInput13Controller extends XInputController {
|
||||||
protected byte[] data;
|
public XInput13Controller(String identifier) throws ControllerInitializationFailedException {
|
||||||
|
super(ControllerType.XINPUT13, identifier);
|
||||||
public ControllerData(short vid, short pid, int handle, byte[] data) {
|
}
|
||||||
this.vid = vid;
|
|
||||||
this.pid = pid;
|
|
||||||
this.data = data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ControllerData() {}
|
|
||||||
|
|
||||||
public short getVID() {
|
|
||||||
return vid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public short getPID() {
|
|
||||||
return pid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] getData() {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
package net.ash.HIDToVPADNetworkClient.controller;
|
||||||
|
|
||||||
|
import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException;
|
||||||
|
|
||||||
|
public class XInput14Controller extends XInputController {
|
||||||
|
public XInput14Controller(String identifier) throws ControllerInitializationFailedException {
|
||||||
|
super(ControllerType.XINPUT14, identifier);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,128 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
package net.ash.HIDToVPADNetworkClient.controller;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
import com.ivan.xinput.XInputAxes;
|
||||||
|
import com.ivan.xinput.XInputButtons;
|
||||||
|
import com.ivan.xinput.XInputComponents;
|
||||||
|
import com.ivan.xinput.XInputDevice;
|
||||||
|
import com.ivan.xinput.exceptions.XInputNotLoadedException;
|
||||||
|
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.util.Utilities;
|
||||||
|
|
||||||
|
public class XInputController extends Controller {
|
||||||
|
//the pad number will be appended to this String.
|
||||||
|
public static final String XINPUT_INDENTIFER = "\\\\?\\XINPUT\\";
|
||||||
|
|
||||||
|
@Getter @Setter(AccessLevel.PRIVATE) private XInputDevice device;
|
||||||
|
|
||||||
|
public XInputController(ControllerType type, String identifier) throws ControllerInitializationFailedException {
|
||||||
|
super(type, identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean initController(String identifier) {
|
||||||
|
int pad = Integer.parseInt(identifier.substring(XINPUT_INDENTIFER.length()));
|
||||||
|
XInputDevice device = null;
|
||||||
|
try {
|
||||||
|
device = XInputDevice.getDeviceFor(pad);
|
||||||
|
} catch (XInputNotLoadedException e) {
|
||||||
|
//Log?
|
||||||
|
}
|
||||||
|
if(device == null) return false;
|
||||||
|
setDevice(device);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] pollLatestData() {
|
||||||
|
if(device.poll()){
|
||||||
|
ByteBuffer data = ByteBuffer.allocate(8);
|
||||||
|
XInputComponents components = device.getComponents();
|
||||||
|
|
||||||
|
XInputButtons buttons = components.getButtons();
|
||||||
|
|
||||||
|
int buttonState = 0;
|
||||||
|
if(buttons.a) buttonState |= (1 << 0);
|
||||||
|
if(buttons.b) buttonState |= (1 << 1);
|
||||||
|
if(buttons.x) buttonState |= (1 << 2);
|
||||||
|
if(buttons.y) buttonState |= (1 << 3);
|
||||||
|
|
||||||
|
if(buttons.left) buttonState |= (1 << 4);
|
||||||
|
if(buttons.up) buttonState |= (1 << 5);
|
||||||
|
if(buttons.right) buttonState |= (1 << 6);
|
||||||
|
if(buttons.down) buttonState |= (1 << 7);
|
||||||
|
|
||||||
|
if(buttons.back) buttonState |= (1 << 8);
|
||||||
|
if(buttons.start) buttonState |= (1 << 9);
|
||||||
|
if(buttons.lShoulder) buttonState |= (1 << 10);
|
||||||
|
if(buttons.rShoulder) buttonState |= (1 << 11);
|
||||||
|
if(buttons.lThumb) buttonState |= (1 << 12);
|
||||||
|
if(buttons.rThumb) buttonState |= (1 << 13);
|
||||||
|
if(buttons.unknown) buttonState |= (1 << 14);
|
||||||
|
if (XInputDevice.isGuideButtonSupported()) {
|
||||||
|
if (buttons.guide) buttonState |= (1 << 15);
|
||||||
|
}
|
||||||
|
|
||||||
|
XInputAxes axes = components.getAxes();
|
||||||
|
int axesData = 0;
|
||||||
|
|
||||||
|
axesData |= Utilities.signedShortToByte(axes.lxRaw) << 24;
|
||||||
|
axesData |= Utilities.signedShortToByte(axes.lyRaw) << 16;
|
||||||
|
axesData |= Utilities.signedShortToByte(axes.rxRaw) << 8;
|
||||||
|
axesData |= Utilities.signedShortToByte(axes.ryRaw) << 0;
|
||||||
|
|
||||||
|
short axesDataShoulderButtons = 0;
|
||||||
|
|
||||||
|
axesDataShoulderButtons |= axes.ltRaw << 8;
|
||||||
|
axesDataShoulderButtons |= axes.rtRaw << 0;
|
||||||
|
|
||||||
|
buttonState |= axesDataShoulderButtons << 16;
|
||||||
|
data.putInt(axesData).putInt(buttonState);
|
||||||
|
|
||||||
|
return(data.array());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroyDriver() {
|
||||||
|
//not needed
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: Other values for VID/PID? I guess other people had this idea too...
|
||||||
|
@Override
|
||||||
|
public short getVID() {
|
||||||
|
return 0x7331;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short getPID() {
|
||||||
|
return 0x1337;
|
||||||
|
}
|
||||||
|
}
|
@ -19,9 +19,12 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package net.ash.HIDToVPADNetworkClient.gui;
|
package net.ash.HIDToVPADNetworkClient.exeption;
|
||||||
|
|
||||||
public enum GuiControllerType {
|
public class ControllerInitializationFailedException extends Exception {
|
||||||
HID4JAVA,
|
|
||||||
LINUX
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
}
|
}
|
@ -1,89 +0,0 @@
|
|||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*******************************************************************************/
|
|
||||||
package net.ash.HIDToVPADNetworkClient.gui;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FilenameFilter;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.hid4java.HidDevice;
|
|
||||||
import org.hid4java.HidManager;
|
|
||||||
|
|
||||||
public class ControllerDetector extends Thread {
|
|
||||||
private static final int POLL_TIMEOUT = 1000;
|
|
||||||
private static final int SHORT_POLL_TIMEOUT = 100;
|
|
||||||
|
|
||||||
private int lastHashCode = 0;
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
for (;;) {
|
|
||||||
if (GuiMain.instance() != null) {
|
|
||||||
List<GuiController> tmp = detectControllers();
|
|
||||||
if (lastHashCode != tmp.hashCode()) {
|
|
||||||
lastHashCode = tmp.hashCode();
|
|
||||||
GuiMain.instance().updateControllerList(tmp);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Thread.sleep(POLL_TIMEOUT);
|
|
||||||
} catch (InterruptedException e) {}
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
Thread.sleep(SHORT_POLL_TIMEOUT);
|
|
||||||
} catch (InterruptedException e) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<GuiController> detectControllers() {
|
|
||||||
List<GuiController> controllers = new ArrayList<GuiController>();
|
|
||||||
|
|
||||||
String os = System.getProperty("os.name");
|
|
||||||
//System.out.println("[ControllerDetector] OS: " + os);
|
|
||||||
|
|
||||||
if (os.contains("Linux")) {
|
|
||||||
detectLinuxControllers(controllers);
|
|
||||||
} else if (os.contains("Windows")) {
|
|
||||||
System.out.println("Running on Windows! XInput coming soon."); //XXX debug text (win32)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (HidDevice device : HidManager.getHidServices().getAttachedHidDevices()) {
|
|
||||||
controllers.add(new GuiController(GuiControllerType.HID4JAVA, device.getPath()));
|
|
||||||
}
|
|
||||||
return controllers;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void detectLinuxControllers(List<GuiController> controllers) {
|
|
||||||
File devInput = new File("/dev/input");
|
|
||||||
if (!devInput.exists()) return;
|
|
||||||
|
|
||||||
File[] linuxControllers = devInput.listFiles(new FilenameFilter() {
|
|
||||||
public boolean accept(File dir, String name) {
|
|
||||||
return name.startsWith("js"); //js0, js1, etc...
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
for (File controller : linuxControllers) {
|
|
||||||
controllers.add(new GuiController(GuiControllerType.LINUX, controller.getAbsolutePath()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -25,29 +25,29 @@ import java.awt.event.WindowEvent;
|
|||||||
import java.awt.event.WindowListener;
|
import java.awt.event.WindowListener;
|
||||||
|
|
||||||
import net.ash.HIDToVPADNetworkClient.Main;
|
import net.ash.HIDToVPADNetworkClient.Main;
|
||||||
|
|
||||||
public class GuiCloseListener implements WindowListener {
|
public class GuiCloseListener implements WindowListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void windowClosing(WindowEvent arg0) {
|
public void windowClosing(WindowEvent arg0) {
|
||||||
Main.initiateShutdown();
|
Main.initiateShutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void windowActivated(WindowEvent arg0) {}
|
public void windowActivated(WindowEvent arg0) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void windowClosed(WindowEvent arg0) {}
|
public void windowClosed(WindowEvent arg0) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void windowDeactivated(WindowEvent arg0) {}
|
public void windowDeactivated(WindowEvent arg0) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void windowDeiconified(WindowEvent arg0) {}
|
public void windowDeiconified(WindowEvent arg0) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void windowIconified(WindowEvent arg0) {}
|
public void windowIconified(WindowEvent arg0) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void windowOpened(WindowEvent arg0) {}
|
public void windowOpened(WindowEvent arg0) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,21 +23,25 @@ package net.ash.HIDToVPADNetworkClient.gui;
|
|||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.swing.AbstractButton;
|
|
||||||
import javax.swing.BoxLayout;
|
import javax.swing.BoxLayout;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.Timer;
|
||||||
|
|
||||||
|
import net.ash.HIDToVPADNetworkClient.controller.Controller;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.manager.ControllerManager;
|
||||||
|
|
||||||
public class GuiControllerList extends JPanel {
|
public class GuiControllerList extends JPanel {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private JPanel innerScrollPanel;
|
private JPanel innerScrollPanel;
|
||||||
private ActionListener currentActionListener = null;
|
|
||||||
|
|
||||||
public GuiControllerList() {
|
public GuiControllerList() {
|
||||||
super(new BorderLayout());
|
super(new BorderLayout());
|
||||||
@ -45,62 +49,60 @@ public class GuiControllerList extends JPanel {
|
|||||||
innerScrollPanel = new JPanel();
|
innerScrollPanel = new JPanel();
|
||||||
innerScrollPanel.setLayout(new BoxLayout(innerScrollPanel, BoxLayout.PAGE_AXIS));
|
innerScrollPanel.setLayout(new BoxLayout(innerScrollPanel, BoxLayout.PAGE_AXIS));
|
||||||
add(new JScrollPane(innerScrollPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
|
add(new JScrollPane(innerScrollPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
|
||||||
|
|
||||||
|
int delay = 1000; //milliseconds
|
||||||
|
ActionListener taskPerformer = new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
updateControllerList();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
new Timer(delay, taskPerformer).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized List<GuiController> getControllers() {
|
public synchronized void updateControllerList() {
|
||||||
List<GuiController> controllers = new ArrayList<GuiController>();
|
//System.out.println("[GuiControllerList] Updating controller list..."); //XXX debug text
|
||||||
|
|
||||||
for (Component component : innerScrollPanel.getComponents()) {
|
boolean repaintNeeded = false;
|
||||||
if (component instanceof GuiControllerListItem) {
|
|
||||||
controllers.add(((GuiControllerListItem)component).getData());
|
List<Controller> attachedControllers = ControllerManager.getAttachedControllers();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return controllers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void updateControllerList(List<GuiController> controllers) {
|
|
||||||
System.out.println("[GuiControllerList] Updating controller list..."); //XXX debug text
|
|
||||||
|
|
||||||
HashMap<Integer, GuiController> components = new HashMap<Integer, GuiController>();
|
|
||||||
for (Component component : innerScrollPanel.getComponents()) {
|
|
||||||
if (component instanceof GuiControllerListItem) {
|
|
||||||
components.put(component.hashCode(), ((GuiControllerListItem)component).getData());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
List<GuiControllerListItem> newComponents = new ArrayList<GuiControllerListItem>();
|
List<GuiControllerListItem> newComponents = new ArrayList<GuiControllerListItem>();
|
||||||
for (GuiController controller : controllers) {
|
|
||||||
GuiControllerListItem i;
|
Map<Controller,GuiControllerListItem> components = new HashMap<>();
|
||||||
if (components.containsKey(controller.hashCode())) {
|
for (Component component : innerScrollPanel.getComponents()) {
|
||||||
i = new GuiControllerListItem(components.get(controller.hashCode()));
|
if (component instanceof GuiControllerListItem) {
|
||||||
} else {
|
GuiControllerListItem comp = (GuiControllerListItem) component;
|
||||||
|
Controller cont = comp.getController();
|
||||||
|
if(attachedControllers.contains(cont)){
|
||||||
|
components.put(cont,comp);
|
||||||
|
}else{//Controller removed
|
||||||
|
repaintNeeded = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Build new list of components.
|
||||||
|
for (Controller controller : attachedControllers) {
|
||||||
|
GuiControllerListItem i = null;
|
||||||
|
if (components.containsKey(controller)) {
|
||||||
|
newComponents.add(components.get(controller));
|
||||||
|
}else{ //New controller was added
|
||||||
|
repaintNeeded = true;
|
||||||
i = new GuiControllerListItem(controller);
|
i = new GuiControllerListItem(controller);
|
||||||
|
newComponents.add(i);
|
||||||
}
|
}
|
||||||
newComponents.add(i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
innerScrollPanel.removeAll();
|
if(repaintNeeded){
|
||||||
for (GuiControllerListItem component : newComponents) {
|
innerScrollPanel.removeAll();
|
||||||
component.addActionListener(currentActionListener);
|
for (GuiControllerListItem component : newComponents) {
|
||||||
innerScrollPanel.add(component);
|
innerScrollPanel.add(component);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO research performance impact - 300+ms on swing.RepaintManager?
|
innerScrollPanel.revalidate();
|
||||||
innerScrollPanel.revalidate();
|
revalidate();
|
||||||
revalidate();
|
innerScrollPanel.repaint();
|
||||||
innerScrollPanel.repaint();
|
repaint();
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void setActionListener(ActionListener l) {
|
|
||||||
currentActionListener = l;
|
|
||||||
for (Component c : innerScrollPanel.getComponents()) {
|
|
||||||
try {
|
|
||||||
((AbstractButton)c).addActionListener(l);
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
System.out.println("[GuiControllerList] Bad cast on " + c.getClass().getName() + " to AbstractButton!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,52 +23,91 @@ package net.ash.HIDToVPADNetworkClient.gui;
|
|||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
import javax.swing.JCheckBox;
|
import javax.swing.JCheckBox;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.Timer;
|
||||||
|
|
||||||
public class GuiControllerListItem extends JPanel {
|
import lombok.Getter;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.controller.Controller;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
|
||||||
|
|
||||||
|
public class GuiControllerListItem extends JPanel implements ActionListener {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private GuiController data = null;
|
@Getter private final Controller controller;
|
||||||
private JCheckBox checkbox;
|
private JCheckBox checkbox;
|
||||||
|
|
||||||
public GuiControllerListItem(GuiController data) {
|
public GuiControllerListItem(Controller data) {
|
||||||
super(new BorderLayout());
|
super(new BorderLayout());
|
||||||
|
|
||||||
setMinimumSize(new Dimension (300, 30));
|
setMinimumSize(new Dimension (300, 30));
|
||||||
setPreferredSize(new Dimension(300, 30));
|
setPreferredSize(new Dimension(300, 30));
|
||||||
setMaximumSize(new Dimension(2000, 30));
|
setMaximumSize(new Dimension(2000, 30));
|
||||||
|
|
||||||
this.data = data;
|
this.controller = data;
|
||||||
|
|
||||||
checkbox = new JCheckBox(getFlavorText());
|
checkbox = new JCheckBox(getFlavorText());
|
||||||
checkbox.setSelected(data.getActiveState());
|
checkbox.setSelected(data.isActive());
|
||||||
|
checkbox.addActionListener(this);
|
||||||
add(checkbox);
|
add(checkbox);
|
||||||
|
|
||||||
|
int delay = 100; //milliseconds
|
||||||
|
ActionListener taskPerformer = new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
checkbox.setEnabled(NetworkManager.getInstance().isConnected());
|
||||||
|
checkbox.setSelected(controller.isActive());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
new Timer(delay, taskPerformer).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getFlavorText() {
|
private String getFlavorText() {
|
||||||
switch (data.getType()) {
|
switch (controller.getType()) { //TODO: String.format with value from Settings.
|
||||||
case HID4JAVA:
|
case XINPUT13:
|
||||||
return "USB HID on " + data.getId().toString();
|
return "XInput 1.3 on " + controller.getIdentifier();
|
||||||
case LINUX:
|
case XINPUT14:
|
||||||
return "Linux controller on " + data.getId().toString();
|
return "XInput 1.4 on " + controller.getIdentifier();
|
||||||
default:
|
case HID4JAVA:
|
||||||
return data.toString();
|
return "USB HID on " + controller.getIdentifier();
|
||||||
|
case LINUX:
|
||||||
|
return "Linux controller on " + controller.getIdentifier();
|
||||||
|
default:
|
||||||
|
return controller.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addActionListener(ActionListener l) {
|
@Override
|
||||||
checkbox.addActionListener(l);
|
public void actionPerformed(ActionEvent e) {
|
||||||
}
|
boolean selected = ((JCheckBox) e.getSource()).isSelected();
|
||||||
|
controller.setActive(selected);
|
||||||
public GuiController getData() {
|
checkbox.setSelected(controller.isActive());
|
||||||
return data;
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
@Override
|
public int hashCode() {
|
||||||
public int hashCode() {
|
final int prime = 31;
|
||||||
return data.hashCode();
|
int result = 1;
|
||||||
}
|
result = prime * result + ((controller == null) ? 0 : controller.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
GuiControllerListItem other = (GuiControllerListItem) obj;
|
||||||
|
if (controller == null) {
|
||||||
|
if (other.controller != null)
|
||||||
|
return false;
|
||||||
|
} else if (!controller.equals(other.controller))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,8 @@ package net.ash.HIDToVPADNetworkClient.gui;
|
|||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.FlowLayout;
|
import java.awt.FlowLayout;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
import javax.swing.Box;
|
import javax.swing.Box;
|
||||||
import javax.swing.BoxLayout;
|
import javax.swing.BoxLayout;
|
||||||
@ -31,19 +33,24 @@ import javax.swing.JButton;
|
|||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.JTextField;
|
import javax.swing.JTextField;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
import javax.swing.Timer;
|
||||||
|
|
||||||
public class GuiInputControls extends JPanel {
|
import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
|
||||||
|
|
||||||
|
public class GuiInputControls extends JPanel implements ActionListener {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private static GuiInputControls instance = null;
|
private static GuiInputControls instance = null;
|
||||||
|
|
||||||
private static final String DEFAULT_PACKET_INTERVAL = "1000";
|
private static final String CONNECT = "Connect";
|
||||||
|
private static final String DISCONNECT = "Disconnect";
|
||||||
|
|
||||||
private JButton connectButton;
|
private JButton connectButton;
|
||||||
private JTextField ipTextBox;
|
private JTextField ipTextBox;
|
||||||
private JPanel ipTextBoxWrap;
|
private JPanel ipTextBoxWrap;
|
||||||
private JTextField packetIntervalTextBox;
|
private JTextField packetIntervalTextBox;
|
||||||
private JPanel piTextBoxWrap;
|
|
||||||
private JLabel statusLabel;
|
private JLabel statusLabel;
|
||||||
|
|
||||||
public GuiInputControls() throws Exception {
|
public GuiInputControls() throws Exception {
|
||||||
super();
|
super();
|
||||||
if (instance != null) {
|
if (instance != null) {
|
||||||
@ -54,33 +61,24 @@ public class GuiInputControls extends JPanel {
|
|||||||
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
|
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
|
||||||
setPreferredSize(new Dimension(220, 150));
|
setPreferredSize(new Dimension(220, 150));
|
||||||
|
|
||||||
connectButton = new JButton("Connect");
|
connectButton = new JButton(CONNECT);
|
||||||
connectButton.setAlignmentX(Component.CENTER_ALIGNMENT);
|
connectButton.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
|
||||||
ipTextBox = new JTextField();
|
ipTextBox = new JTextField();
|
||||||
ipTextBox.setColumns(15);
|
ipTextBox.setColumns(15);
|
||||||
|
ipTextBox.setText("192.168.0.35");
|
||||||
ipTextBoxWrap = new JPanel(new FlowLayout());
|
ipTextBoxWrap = new JPanel(new FlowLayout());
|
||||||
ipTextBoxWrap.add(new JLabel("IP: "));
|
ipTextBoxWrap.add(new JLabel("IP: "));
|
||||||
ipTextBoxWrap.add(ipTextBox);
|
ipTextBoxWrap.add(ipTextBox);
|
||||||
ipTextBoxWrap.setMaximumSize(new Dimension(1000, 20));
|
ipTextBoxWrap.setMaximumSize(new Dimension(1000, 20));
|
||||||
|
|
||||||
packetIntervalTextBox = new JTextField();
|
|
||||||
packetIntervalTextBox.setColumns(3);
|
|
||||||
packetIntervalTextBox.setText(DEFAULT_PACKET_INTERVAL);
|
|
||||||
//TODO sanitize input
|
|
||||||
piTextBoxWrap = new JPanel(new FlowLayout());
|
|
||||||
piTextBoxWrap.add(new JLabel("Packet interval: "));
|
|
||||||
piTextBoxWrap.add(packetIntervalTextBox);
|
|
||||||
piTextBoxWrap.setMaximumSize(new Dimension(1000, 20));
|
|
||||||
|
|
||||||
statusLabel = new JLabel("Ready.");
|
statusLabel = new JLabel("Ready.");
|
||||||
statusLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
statusLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
add(Box.createVerticalGlue());
|
add(Box.createVerticalGlue());
|
||||||
|
|
||||||
add(ipTextBoxWrap);
|
add(ipTextBoxWrap);
|
||||||
add(piTextBoxWrap);
|
|
||||||
|
|
||||||
add(Box.createRigidArea(new Dimension(1, 4)));
|
add(Box.createRigidArea(new Dimension(1, 4)));
|
||||||
add(connectButton);
|
add(connectButton);
|
||||||
@ -90,7 +88,19 @@ public class GuiInputControls extends JPanel {
|
|||||||
|
|
||||||
add(Box.createVerticalGlue());
|
add(Box.createVerticalGlue());
|
||||||
|
|
||||||
connectButton.addActionListener(GuiInteractionManager.instance());
|
connectButton.addActionListener(this);
|
||||||
|
|
||||||
|
int delay = 100; //milliseconds
|
||||||
|
ActionListener taskPerformer = new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
if(NetworkManager.getInstance().isConnected()){
|
||||||
|
connectButton.setText(DISCONNECT);
|
||||||
|
}else{
|
||||||
|
connectButton.setText(CONNECT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
new Timer(delay, taskPerformer).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GuiInputControls instance() {
|
public static GuiInputControls instance() {
|
||||||
@ -112,4 +122,18 @@ public class GuiInputControls extends JPanel {
|
|||||||
public JLabel getStatusLabel() {
|
public JLabel getStatusLabel() {
|
||||||
return statusLabel;
|
return statusLabel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
if(NetworkManager.getInstance().isConnected()){
|
||||||
|
NetworkManager.getInstance().disconnect();
|
||||||
|
}else{
|
||||||
|
NetworkManager.getInstance().connect(ipTextBox.getText());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,105 +0,0 @@
|
|||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*******************************************************************************/
|
|
||||||
package net.ash.HIDToVPADNetworkClient.gui;
|
|
||||||
|
|
||||||
import java.awt.Component;
|
|
||||||
import java.awt.Container;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
|
|
||||||
import javax.swing.JButton;
|
|
||||||
import javax.swing.JCheckBox;
|
|
||||||
|
|
||||||
import net.ash.HIDToVPADNetworkClient.controller.ControllerManager;
|
|
||||||
import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
|
|
||||||
|
|
||||||
public class GuiInteractionManager implements ActionListener {
|
|
||||||
private static GuiInteractionManager instance = null;
|
|
||||||
|
|
||||||
public GuiInteractionManager() throws Exception {
|
|
||||||
if (instance != null) {
|
|
||||||
throw new Exception("GuiInputControls already has an instance!");
|
|
||||||
}
|
|
||||||
instance = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
/*
|
|
||||||
* Swing GUI events
|
|
||||||
*/
|
|
||||||
if (e.getSource() instanceof Component) {
|
|
||||||
Component source = (Component)e.getSource();
|
|
||||||
Container parent = source.getParent();
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Action handler code for GuiControllerListItem
|
|
||||||
*/
|
|
||||||
if (parent instanceof GuiControllerListItem) {
|
|
||||||
GuiControllerListItem rparent = (GuiControllerListItem)parent;
|
|
||||||
JCheckBox rsource = (JCheckBox)source;
|
|
||||||
|
|
||||||
rparent.getData().setActiveState(rsource.isSelected());
|
|
||||||
|
|
||||||
if (NetworkManager.instance().isRunning()) {
|
|
||||||
ControllerManager.instance().updateControllers(GuiMain.instance().getControllers());
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
* Action handler for GuiInputControls
|
|
||||||
*/
|
|
||||||
} else if (parent instanceof GuiInputControls) {
|
|
||||||
GuiInputControls rparent = (GuiInputControls)parent;
|
|
||||||
/*
|
|
||||||
* (Dis)connect button
|
|
||||||
*/
|
|
||||||
if (source instanceof JButton) {
|
|
||||||
if (NetworkManager.instance().isRunning()) {
|
|
||||||
disconnect();
|
|
||||||
} else {
|
|
||||||
NetworkManager.instance().setPacketInterval(Integer.parseInt(rparent.getPacketIntervalTextBox().getText()));
|
|
||||||
if (ControllerManager.instance().startConnection(GuiMain.instance().getIPText(), GuiMain.instance().getControllers())) {
|
|
||||||
rparent.getIpTextBox().setEnabled(false);
|
|
||||||
rparent.getPacketIntervalTextBox().setEnabled(false);
|
|
||||||
rparent.getConnectButton().setText("Disconnect");
|
|
||||||
GuiInputControls.instance().getStatusLabel().setText("Connected!");
|
|
||||||
} else {
|
|
||||||
ControllerManager.instance().stopConnection();
|
|
||||||
GuiInputControls.instance().getStatusLabel().setText("Connection Failed!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void disconnect() {
|
|
||||||
ControllerManager.instance().stopConnection();
|
|
||||||
GuiInputControls.instance().getIpTextBox().setEnabled(true);
|
|
||||||
GuiInputControls.instance().getPacketIntervalTextBox().setEnabled(true);
|
|
||||||
GuiInputControls.instance().getConnectButton().setText("Connect");
|
|
||||||
GuiInputControls.instance().getStatusLabel().setText("Disconnected.");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static GuiInteractionManager instance() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
}
|
|
@ -23,7 +23,6 @@ package net.ash.HIDToVPADNetworkClient.gui;
|
|||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
@ -54,18 +53,9 @@ public class GuiMain extends JPanel {
|
|||||||
public GuiMain() {
|
public GuiMain() {
|
||||||
super(new BorderLayout());
|
super(new BorderLayout());
|
||||||
|
|
||||||
try {
|
|
||||||
new GuiInteractionManager();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Main.fatal();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
leftControllerList = new GuiControllerList();
|
leftControllerList = new GuiControllerList();
|
||||||
leftControllerList.setPreferredSize(new Dimension(300, 100));
|
leftControllerList.setPreferredSize(new Dimension(300, 100));
|
||||||
add(leftControllerList, BorderLayout.CENTER);
|
add(leftControllerList, BorderLayout.CENTER);
|
||||||
leftControllerList.setActionListener(GuiInteractionManager.instance());
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
rightSideControls = new GuiInputControls();
|
rightSideControls = new GuiInputControls();
|
||||||
@ -76,18 +66,6 @@ public class GuiMain extends JPanel {
|
|||||||
add(rightSideControls, BorderLayout.LINE_END);
|
add(rightSideControls, BorderLayout.LINE_END);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateControllerList(List<GuiController> controllers) {
|
|
||||||
leftControllerList.updateControllerList(controllers);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<GuiController> getControllers() {
|
|
||||||
return leftControllerList.getControllers();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getIPText() {
|
|
||||||
return rightSideControls.getIpTextBox().getText();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static GuiMain instance() {
|
public static GuiMain instance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,132 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
package net.ash.HIDToVPADNetworkClient.manager;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
import net.ash.HIDToVPADNetworkClient.controller.Controller;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.NetworkHIDDevice;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.util.Settings;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.util.Utilities;
|
||||||
|
|
||||||
|
public class ActiveControllerManager implements Runnable{
|
||||||
|
private static ActiveControllerManager instance = new ActiveControllerManager();
|
||||||
|
|
||||||
|
private ActiveControllerManager(){
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ActiveControllerManager getInstance(){
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() { //TODO: Add mechanism to stop these threads?
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while(true){
|
||||||
|
updateControllerStates();
|
||||||
|
ControllerManager.detectControllers();
|
||||||
|
Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while(true){
|
||||||
|
handleControllerInputs();
|
||||||
|
Utilities.sleep(Settings.HANDLE_INPUTS_INTERVAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Map<Controller,NetworkHIDDevice> activeControllers = new HashMap<>();
|
||||||
|
public void updateControllerStates() {
|
||||||
|
List<Controller> currentControllers = ControllerManager.getActiveControllers();
|
||||||
|
|
||||||
|
List<Controller> toAdd = new ArrayList<>();
|
||||||
|
List<Controller> toRemove = new ArrayList<>();
|
||||||
|
synchronized (activeControllers) {
|
||||||
|
//Adding all missing.
|
||||||
|
|
||||||
|
for(Controller c: currentControllers){
|
||||||
|
if(!activeControllers.containsKey(c)){
|
||||||
|
System.out.println("Added " + c); //TODO: real logging
|
||||||
|
toAdd.add(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//removing all old
|
||||||
|
for(Controller c : activeControllers.keySet()){
|
||||||
|
if(!currentControllers.contains(c)){
|
||||||
|
System.out.println("Removed " + c); //TODO: real logging
|
||||||
|
toRemove.add(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addController(toAdd);
|
||||||
|
removeController(toRemove);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeController(List<Controller> toRemove) {
|
||||||
|
synchronized (activeControllers) {
|
||||||
|
for(Controller c : toRemove){
|
||||||
|
NetworkManager.getInstance().removeHIDDevice(activeControllers.get(c));
|
||||||
|
c.destroyDriver();
|
||||||
|
activeControllers.remove(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addController(List<Controller> toAdd) {
|
||||||
|
synchronized (activeControllers) {
|
||||||
|
for(Controller c : toAdd){
|
||||||
|
NetworkHIDDevice hiddevice = new NetworkHIDDevice(c.getVID(), c.getPID());
|
||||||
|
hiddevice.sendAttach();
|
||||||
|
NetworkManager.getInstance().addHIDDevice(hiddevice);
|
||||||
|
activeControllers.put(c,hiddevice);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleControllerInputs() {
|
||||||
|
synchronized (activeControllers) {
|
||||||
|
for(Entry<Controller, NetworkHIDDevice> entry : activeControllers.entrySet()){
|
||||||
|
byte[] data = entry.getKey().getLatestData();
|
||||||
|
if(data != null){
|
||||||
|
NetworkHIDDevice device = entry.getValue();
|
||||||
|
device.sendRead(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,203 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
package net.ash.HIDToVPADNetworkClient.manager;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
import org.hid4java.HidDevice;
|
||||||
|
import org.hid4java.HidManager;
|
||||||
|
|
||||||
|
import com.ivan.xinput.XInputDevice;
|
||||||
|
import com.ivan.xinput.XInputDevice14;
|
||||||
|
import com.ivan.xinput.exceptions.XInputNotLoadedException;
|
||||||
|
|
||||||
|
import lombok.Synchronized;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.controller.Controller;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.controller.Controller.ControllerType;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.controller.Hid4JavaController;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.controller.LinuxDevInputController;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.controller.XInput13Controller;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.controller.XInput14Controller;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.controller.XInputController;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException;
|
||||||
|
|
||||||
|
public class ControllerManager{
|
||||||
|
private static Map<String,Controller> attachedControllers = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detects all attached controller.
|
||||||
|
*/
|
||||||
|
@Synchronized("attachedControllers")
|
||||||
|
public static void detectControllers() {
|
||||||
|
String os = System.getProperty("os.name");
|
||||||
|
//System.out.println("[ControllerDetector] OS: " + os);
|
||||||
|
|
||||||
|
Map<String,ControllerType> connectedDevices = new HashMap<>();
|
||||||
|
|
||||||
|
if (os.contains("Linux")) {
|
||||||
|
connectedDevices.putAll(detectLinuxControllers());
|
||||||
|
} else if (os.contains("Windows")) {
|
||||||
|
connectedDevices.putAll(detectWindowsControllers());
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: Enable HID4Java again
|
||||||
|
//connectedDevices.putAll(detectHIDDevices());
|
||||||
|
|
||||||
|
//Remove detached devices
|
||||||
|
List<String> toRemove = new ArrayList<>();
|
||||||
|
for(String s : attachedControllers.keySet()){
|
||||||
|
if(!connectedDevices.containsKey(s)){
|
||||||
|
toRemove.add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(String remove : toRemove){
|
||||||
|
attachedControllers.get(remove).destroyAll();
|
||||||
|
attachedControllers.remove(remove);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Add attached devices!
|
||||||
|
for(Entry<String, ControllerType> entry : connectedDevices.entrySet()){
|
||||||
|
String deviceIdentifier = entry.getKey();
|
||||||
|
if(!attachedControllers.containsKey(deviceIdentifier)){
|
||||||
|
Controller c = null;
|
||||||
|
switch(entry.getValue()){
|
||||||
|
case HID4JAVA:
|
||||||
|
try {
|
||||||
|
c= new Hid4JavaController(deviceIdentifier);
|
||||||
|
} catch (ControllerInitializationFailedException e) {
|
||||||
|
//e.printStackTrace();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LINUX:
|
||||||
|
try {
|
||||||
|
c = new LinuxDevInputController(deviceIdentifier);
|
||||||
|
} catch (ControllerInitializationFailedException e) {
|
||||||
|
//e.printStackTrace();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XINPUT14:
|
||||||
|
try {
|
||||||
|
c = new XInput14Controller(deviceIdentifier);
|
||||||
|
} catch (ControllerInitializationFailedException e) {
|
||||||
|
//e.printStackTrace();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XINPUT13:
|
||||||
|
try {
|
||||||
|
c = new XInput13Controller(deviceIdentifier);
|
||||||
|
} catch (ControllerInitializationFailedException e) {
|
||||||
|
//e.printStackTrace();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(c != null){ //I don't like that starting the Thread happens here =/
|
||||||
|
new Thread(c).start();
|
||||||
|
attachedControllers.put(deviceIdentifier,c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized("attachedControllers")
|
||||||
|
public static List<Controller> getAttachedControllers() {
|
||||||
|
return new ArrayList<>(attachedControllers.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, ControllerType> detectHIDDevices() {
|
||||||
|
Map<String,ControllerType> connectedDevices = new HashMap<>();
|
||||||
|
for (HidDevice device : HidManager.getHidServices().getAttachedHidDevices()) {
|
||||||
|
if(device.getUsage() == 0x05 || device.getUsage() == 0x04){
|
||||||
|
connectedDevices.put(device.getPath(),ControllerType.HID4JAVA);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return connectedDevices;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, ControllerType> detectWindowsControllers() {
|
||||||
|
Map<String,ControllerType> result = new HashMap<>();
|
||||||
|
ControllerType type = ControllerType.XINPUT13;
|
||||||
|
if(XInputDevice.isAvailable() || XInputDevice14.isAvailable()) {
|
||||||
|
if(XInputDevice14.isAvailable()){
|
||||||
|
type = ControllerType.XINPUT14;
|
||||||
|
}
|
||||||
|
for(int i =0;i<4;i++){
|
||||||
|
XInputDevice device;
|
||||||
|
try {
|
||||||
|
device = XInputDevice.getDeviceFor(i);
|
||||||
|
if(device.poll() && device.isConnected()){ //Check if it is this controller is connected
|
||||||
|
result.put(XInputController.XINPUT_INDENTIFER + i, type);
|
||||||
|
}
|
||||||
|
} catch (XInputNotLoadedException e) {
|
||||||
|
//This shouln't happen?
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, ControllerType> detectLinuxControllers() {
|
||||||
|
Map<String,ControllerType> result = new HashMap<>();
|
||||||
|
File devInput = new File("/dev/input");
|
||||||
|
if (!devInput.exists()) return result;
|
||||||
|
|
||||||
|
File[] linuxControllers = devInput.listFiles(new FilenameFilter() {
|
||||||
|
public boolean accept(File dir, String name) {
|
||||||
|
return name.startsWith("js"); //js0, js1, etc...
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for (File controller : linuxControllers) {
|
||||||
|
result.put(controller.getAbsolutePath(),ControllerType.LINUX);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized("attachedControllers")
|
||||||
|
public static List<Controller> getActiveControllers() {
|
||||||
|
List<Controller> active = new ArrayList<>();
|
||||||
|
for(Controller c : attachedControllers.values()){
|
||||||
|
if(c.isActive()){
|
||||||
|
active.add(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized("attachedControllers")
|
||||||
|
public static void deactivateAllAttachedControllers() {
|
||||||
|
for(Controller c : attachedControllers.values()){
|
||||||
|
c.setActive(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,99 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
package net.ash.HIDToVPADNetworkClient.network;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.AttachCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.DetachCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.DeviceCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.ReadCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.util.HandleFoundry;
|
||||||
|
|
||||||
|
public class NetworkHIDDevice {
|
||||||
|
@Getter private final short vid;
|
||||||
|
@Getter private final short pid;
|
||||||
|
|
||||||
|
@Getter @Setter private short deviceslot;
|
||||||
|
@Getter @Setter private byte padslot;
|
||||||
|
|
||||||
|
@Getter private int hidHandle = HandleFoundry.next();
|
||||||
|
@Getter(AccessLevel.PRIVATE) private List<DeviceCommand> commands = new ArrayList<>();
|
||||||
|
|
||||||
|
@Getter(AccessLevel.PRIVATE) @Setter(AccessLevel.PRIVATE) private ReadCommand latestRead;
|
||||||
|
|
||||||
|
private Object readCommandLock = new Object();
|
||||||
|
|
||||||
|
public NetworkHIDDevice(short vid, short pid){
|
||||||
|
this.vid = vid;
|
||||||
|
this.pid = pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addCommand(DeviceCommand command){
|
||||||
|
this.commands.add(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clearCommands(){
|
||||||
|
this.commands.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendAttach(){
|
||||||
|
addCommand(new AttachCommand(getHidHandle(), getVid(), getPid(),this));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendDetach(){
|
||||||
|
addCommand(new DetachCommand(getHidHandle(),this));
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] lastdata = null;
|
||||||
|
public void sendRead(byte[] data){
|
||||||
|
if(!Arrays.equals(lastdata, data)){
|
||||||
|
synchronized (readCommandLock) {
|
||||||
|
setLatestRead(new ReadCommand(getHidHandle(),data, this)); //Only get the latest Value.
|
||||||
|
}
|
||||||
|
lastdata = data.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<? extends DeviceCommand> getCommandList() {
|
||||||
|
List<DeviceCommand> commands = new ArrayList<>();
|
||||||
|
commands.addAll(getCommands());
|
||||||
|
DeviceCommand lastRead;
|
||||||
|
|
||||||
|
synchronized (readCommandLock) {
|
||||||
|
if((lastRead = getLatestRead()) != null){
|
||||||
|
commands.add(lastRead);
|
||||||
|
setLatestRead(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clearCommands();
|
||||||
|
return commands;
|
||||||
|
}
|
||||||
|
}
|
@ -19,240 +19,293 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
package net.ash.HIDToVPADNetworkClient.network;
|
package net.ash.HIDToVPADNetworkClient.network;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import org.omg.Messaging.SyncScopeHelper;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
|
||||||
|
|
||||||
import net.ash.HIDToVPADNetworkClient.controller.Controller;
|
import lombok.Getter;
|
||||||
import net.ash.HIDToVPADNetworkClient.gui.GuiInteractionManager;
|
import lombok.Synchronized;
|
||||||
|
import lombok.extern.java.Log;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.manager.ControllerManager;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.AttachCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.DetachCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.DeviceCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.PingCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.ReadCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.util.Utilities;
|
||||||
|
|
||||||
public class NetworkManager implements Runnable {
|
@Log
|
||||||
private static final int PING_INTERVAL = 1000;
|
public class NetworkManager implements Runnable{
|
||||||
|
private final TCPClient tcpClient = new TCPClient();
|
||||||
public static int clientID;
|
private UDPClient udpClient = null;
|
||||||
|
|
||||||
private static NetworkManager instance = null;
|
private static NetworkManager instance = null;
|
||||||
|
private NetworkManager() {
|
||||||
private Object controllersLock = new Object();
|
|
||||||
private HashMap<Integer, Controller> controllers = new HashMap<Integer, Controller>();
|
}
|
||||||
|
|
||||||
private Thread networkThread;
|
public static NetworkManager getInstance(){
|
||||||
|
if(instance == null){
|
||||||
private TCPClient tcp;
|
instance = new NetworkManager();
|
||||||
private UDPClient udp;
|
}
|
||||||
|
return instance;
|
||||||
private int packetInterval = 100;
|
}
|
||||||
|
|
||||||
private enum NetworkState {
|
@Getter private List<NetworkHIDDevice> devices = new ArrayList<>();
|
||||||
DISCONNECTED,
|
|
||||||
FRESH, //Connected, no handshake
|
public void addHIDDevice(NetworkHIDDevice device){
|
||||||
CONNECTED
|
if(!getDevices().contains(device)){
|
||||||
}
|
synchronized (devices) {
|
||||||
private NetworkState networkState = NetworkState.DISCONNECTED;
|
getDevices().add(device);
|
||||||
|
}
|
||||||
public NetworkManager() throws Exception {
|
}
|
||||||
if (instance != null) {
|
}
|
||||||
throw new Exception("NetworkManager already has an instance!");
|
|
||||||
}
|
/*
|
||||||
instance = this;
|
* We want to remove them at the end of a cycle. To make sure the detach was send before removing.
|
||||||
|
*/
|
||||||
networkThread = new Thread(this);
|
@Getter private List<NetworkHIDDevice> toRemove = new ArrayList<>();
|
||||||
tcp = new TCPClient();
|
@Synchronized("toRemove")
|
||||||
udp = new UDPClient();
|
public void removeHIDDevice(NetworkHIDDevice device) {
|
||||||
|
device.sendDetach();
|
||||||
pingThread.start();
|
toRemove.add(device);
|
||||||
|
}
|
||||||
clientID = ThreadLocalRandom.current().nextInt();
|
|
||||||
System.out.println("[NetworkManager] clientID: " + clientID);
|
@Override
|
||||||
}
|
public void run() {
|
||||||
|
int i = 0;
|
||||||
private int runLoopCounter = 0;
|
while(true){
|
||||||
@Override
|
proccessCommands();
|
||||||
public void run() {
|
Utilities.sleep(10);
|
||||||
for (;;) {
|
if(i++ > 1000/10){
|
||||||
for (;;) {
|
ping();
|
||||||
/*
|
i = 0;
|
||||||
* Socket is connected, handshake needed
|
}
|
||||||
*/
|
}
|
||||||
if (networkState == NetworkState.FRESH) {
|
}
|
||||||
try {
|
|
||||||
switch (tcp.doHandshake()) {
|
private void ping() {
|
||||||
|
if(isConnected())sendingCommand(new PingCommand());
|
||||||
case BAD_HANDSHAKE:
|
}
|
||||||
tcp.abort();
|
|
||||||
networkState = NetworkState.DISCONNECTED;
|
public void proccessCommands(){
|
||||||
continue;
|
List<DeviceCommand> commands = new ArrayList<>();
|
||||||
|
synchronized (toRemove) {
|
||||||
case NEW_CLIENT:
|
synchronized (devices) {
|
||||||
synchronized (controllersLock) {
|
for(NetworkHIDDevice device : getDevices()){
|
||||||
for (Controller c : controllers.values()) {
|
commands.addAll(device.getCommandList());
|
||||||
tcp.sendAttach(c);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
networkState = NetworkState.CONNECTED;
|
|
||||||
break;
|
if(commands.isEmpty())return;
|
||||||
|
|
||||||
case SAME_CLIENT:
|
//Split up into "read commands" and other commands.
|
||||||
networkState = NetworkState.CONNECTED;
|
List<ReadCommand> readCommands = new ArrayList<>();
|
||||||
break;
|
{
|
||||||
|
for(DeviceCommand command : commands){
|
||||||
default:
|
if(command instanceof ReadCommand){
|
||||||
tcp.abort();
|
readCommands.add((ReadCommand) command);
|
||||||
networkState = NetworkState.DISCONNECTED;
|
}
|
||||||
continue;
|
}
|
||||||
|
commands.removeAll(readCommands);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
if(!readCommands.isEmpty()){
|
||||||
}
|
sendingRead(readCommands);
|
||||||
} else if (networkState == NetworkState.CONNECTED) {
|
}
|
||||||
synchronized (controllersLock) {
|
|
||||||
try {
|
if(!commands.isEmpty()){
|
||||||
udp.send(controllers);
|
for(DeviceCommand command : commands){
|
||||||
} catch (Exception e) {
|
sendingCommand(command);
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
synchronized (toRemove) {
|
||||||
if (runLoopCounter++ == PING_INTERVAL / packetInterval) {
|
synchronized (devices) {
|
||||||
synchronized(pingThread) {
|
for(NetworkHIDDevice d: toRemove){
|
||||||
pingThread.notify();
|
commands.remove(d);
|
||||||
}
|
}
|
||||||
runLoopCounter = 0;
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
sleep(packetInterval);
|
|
||||||
} else if (networkState == NetworkState.DISCONNECTED) break;
|
private void sendingCommand(DeviceCommand command) {
|
||||||
} //for (;;)
|
if(command instanceof AttachCommand){
|
||||||
|
sendAttach((AttachCommand) command);
|
||||||
try {
|
}else if(command instanceof DetachCommand){
|
||||||
synchronized (networkThread) {
|
sendDetach((DetachCommand) command);
|
||||||
networkThread.wait();
|
}else if(command instanceof PingCommand){
|
||||||
}
|
sendPing((PingCommand) command);
|
||||||
} catch (InterruptedException e) {}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private void sendPing(PingCommand command) {
|
||||||
public void connect(String ip) {
|
if(sendTCP(Protocol.getRawPingDataToSend(command))){
|
||||||
System.out.println("[NetworkManager] Connecting to " + ip + "..."); //XXX debug text
|
log.info("PING");
|
||||||
try {
|
}else{
|
||||||
udp.connect(ip);
|
log.info("Sending the PING failed");
|
||||||
tcp.connect(ip);
|
}
|
||||||
} catch (Exception e) {
|
}
|
||||||
System.err.println("[NetworkManager] Couldn't connect to Wii U!");
|
|
||||||
e.printStackTrace();
|
private void sendDetach(DetachCommand command) {
|
||||||
return;
|
byte[] sendData;
|
||||||
}
|
try {
|
||||||
networkState = NetworkState.FRESH;
|
sendData = Protocol.getRawDetachDataToSend(command);
|
||||||
if (networkThread.getState() == Thread.State.NEW) {
|
if(!sendTCP(sendData)){
|
||||||
networkThread.start();
|
log.info("Sending the detach command for device (" + command.getSender() + ") failed. sendTCP failed");
|
||||||
} else if (networkThread.getState() == Thread.State.WAITING) {
|
}
|
||||||
synchronized (networkThread) {
|
} catch (IOException e) {
|
||||||
networkThread.notify();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() {
|
private void sendAttach(AttachCommand command) {
|
||||||
networkState = NetworkState.DISCONNECTED;
|
//Send the TCP command
|
||||||
|
byte[] sendData = null;
|
||||||
if (!Thread.currentThread().equals(networkThread) && networkThread.getState() != Thread.State.NEW) {
|
try {
|
||||||
while (networkThread.getState() != Thread.State.WAITING) {sleep(1);}
|
sendData = Protocol.getRawAttachDataToSend(command);
|
||||||
}
|
}catch (IOException e) {
|
||||||
|
log.info("Building the attach command for device (" + command.getSender() + ") failed." + e.getMessage());
|
||||||
try {
|
return;
|
||||||
tcp.abort();
|
}
|
||||||
} catch (Exception e) {
|
if(sendTCP(sendData)){
|
||||||
e.printStackTrace();
|
byte configFound = 0;
|
||||||
}
|
try {
|
||||||
|
configFound = recvTCPByte();
|
||||||
System.out.println("[NetworkManager] Disconnected.");
|
} catch (IOException e1) {
|
||||||
}
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
/*
|
if(configFound == Protocol.TCP_CMD_ATTACH_CONFIG_FOUND){
|
||||||
* Is there an active connection?
|
log.info("Config on the console found!");
|
||||||
*/
|
}else if(configFound == Protocol.TCP_CMD_ATTACH_CONFIG_NOT_FOUND){
|
||||||
public boolean isConnected() {
|
log.info("NO CONFIG FOUND.");
|
||||||
return networkState == NetworkState.FRESH || networkState == NetworkState.CONNECTED;
|
return;
|
||||||
}
|
}else if (configFound == 0){
|
||||||
|
log.info("Failed to get byte.");
|
||||||
/*
|
disconnect();
|
||||||
* Is the active connection good for data?
|
}
|
||||||
*/
|
|
||||||
public boolean isRunning() {
|
byte userDataOkay = 0;
|
||||||
return networkState == NetworkState.CONNECTED;
|
try {
|
||||||
}
|
userDataOkay = recvTCPByte();
|
||||||
|
} catch (IOException e1) {
|
||||||
public HashMap<Integer,Controller> getControllers() {
|
e1.printStackTrace();
|
||||||
return controllers;
|
}
|
||||||
}
|
if(userDataOkay == Protocol.TCP_CMD_ATTACH_USERDATA_OKAY){
|
||||||
|
log.info("userdata okay!");
|
||||||
public void setPacketInterval(int packetInterval) {
|
}else if(userDataOkay == Protocol.TCP_CMD_ATTACH_USERDATA_BAD){
|
||||||
this.packetInterval = packetInterval;
|
log.info("USERDATA BAD.");
|
||||||
}
|
return;
|
||||||
|
}else if (userDataOkay == 0){
|
||||||
public void addController(Controller controller) {
|
log.info("Failed to get byte.");
|
||||||
synchronized (controllersLock) {
|
disconnect();
|
||||||
if (isRunning()) {
|
}
|
||||||
try {
|
|
||||||
tcp.sendAttach(controller);
|
//We receive our device slot and pad slot
|
||||||
} catch (Exception e) {return;};
|
short deviceslot = -1;
|
||||||
}
|
byte padslot = -1;
|
||||||
controllers.put(controller.getID().hashCode(), controller);
|
try {
|
||||||
}
|
deviceslot = recvTCPShort();
|
||||||
}
|
padslot = recvTCPByte();
|
||||||
|
} catch (IOException e) {
|
||||||
public void removeController(Controller controller) {
|
log.info("Recieving data after sending a attach failed for device (" + command.getSender() + ") failed." + e.getMessage());
|
||||||
synchronized (controllersLock) {
|
}
|
||||||
if (isRunning()) {
|
|
||||||
try {
|
if(deviceslot < 0 || padslot < 0){
|
||||||
tcp.sendDetach(controller);
|
log.info("Recieving data after sending a attach failed for device (" + command.getSender() + ") failed. We need to disconnect =(");
|
||||||
} catch (Exception e) {return;};
|
disconnect();
|
||||||
}
|
}
|
||||||
controller.destroy();
|
|
||||||
controllers.remove(controller.getID().hashCode());
|
//Let's save them for later.
|
||||||
}
|
NetworkHIDDevice sender = command.getSender();
|
||||||
}
|
if(sender != null){
|
||||||
|
sender.setDeviceslot(deviceslot);
|
||||||
public void removeAllControllers() {
|
sender.setPadslot(padslot);
|
||||||
synchronized (controllersLock) {
|
}else{
|
||||||
for (Controller c : controllers.values()) {
|
log.info("Something really went wrong. Got an attach event with out an " + NetworkHIDDevice.class.getSimpleName());
|
||||||
if (isRunning()) {
|
}
|
||||||
try {
|
log.info("Attaching done!");
|
||||||
tcp.sendDetach(c);
|
}else{
|
||||||
} catch (Exception e) {continue;};
|
log.info("Sending the attach command for device (" + command.getSender() + ") failed. sendTCP failed");
|
||||||
}
|
}
|
||||||
c.destroy();
|
}
|
||||||
controllers.remove(c.getID().hashCode());
|
|
||||||
}
|
private void sendingRead(List<ReadCommand> readCommands) {
|
||||||
}
|
byte[] rawCommand;
|
||||||
}
|
try {
|
||||||
|
rawCommand = Protocol.getRawReadDataToSend(readCommands);
|
||||||
private Thread pingThread = new Thread(new Runnable() {
|
System.out.println("UDP Packet: "+ Utilities.ByteArrayToString(rawCommand));
|
||||||
public void run() {
|
sendUDP(rawCommand);
|
||||||
for (;;) {
|
} catch (IOException e) {
|
||||||
synchronized (pingThread) {
|
System.out.println("Sending read data failed.");
|
||||||
try {
|
}
|
||||||
pingThread.wait();
|
}
|
||||||
} catch (InterruptedException e) {}
|
|
||||||
}
|
private boolean sendUDP(byte[] rawCommand) {
|
||||||
|
boolean result = false;
|
||||||
if (!tcp.ping()) {
|
if(udpClient != null){
|
||||||
System.out.println("[NetworkManager] Ping failed, disconnecting...");
|
try {
|
||||||
GuiInteractionManager.instance().disconnect();
|
udpClient.send(rawCommand);
|
||||||
}
|
result = true;
|
||||||
}
|
} catch (Exception e) {
|
||||||
}
|
//
|
||||||
}, "Ping Thread");
|
result = false;
|
||||||
|
}
|
||||||
private void sleep(long ticks) {
|
}
|
||||||
try {
|
return result;
|
||||||
Thread.sleep(ticks);
|
}
|
||||||
} catch (InterruptedException e) {}
|
|
||||||
}
|
private boolean sendTCP(byte[] rawCommand) {
|
||||||
|
boolean result = false;
|
||||||
public static NetworkManager instance() {
|
if(tcpClient != null){
|
||||||
return instance;
|
try {
|
||||||
}
|
tcpClient.send(rawCommand);
|
||||||
|
result = true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
//
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disconnect() {
|
||||||
|
ControllerManager.deactivateAllAttachedControllers();
|
||||||
|
tcpClient.abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
private short recvTCPShort() throws IOException {
|
||||||
|
return tcpClient.recvShort();
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte recvTCPByte() throws IOException {
|
||||||
|
return tcpClient.recvByte();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isConnected() {
|
||||||
|
return (tcpClient != null && tcpClient.isConnected());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean connect(String ip) {
|
||||||
|
boolean result = false;
|
||||||
|
log.info("Trying to connect to: " + ip);
|
||||||
|
try {
|
||||||
|
tcpClient.connect(ip);
|
||||||
|
System.out.println("TCP Connected!");
|
||||||
|
udpClient = UDPClient.createUDPClient(ip);
|
||||||
|
if(udpClient != null){
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error while connecting: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,19 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package net.ash.HIDToVPADNetworkClient.network;
|
package net.ash.HIDToVPADNetworkClient.network;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import lombok.extern.java.Log;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.AttachCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.DetachCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.PingCommand;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.commands.ReadCommand;
|
||||||
|
|
||||||
|
@Log
|
||||||
public class Protocol {
|
public class Protocol {
|
||||||
public static final int TCP_PORT = 8112;
|
public static final int TCP_PORT = 8112;
|
||||||
public static final int UDP_PORT = 8113;
|
public static final int UDP_PORT = 8113;
|
||||||
@ -34,4 +47,65 @@ public class Protocol {
|
|||||||
public static final byte TCP_CMD_PING = (byte)0xF0;
|
public static final byte TCP_CMD_PING = (byte)0xF0;
|
||||||
|
|
||||||
public static final byte UDP_CMD_DATA = 0x03;
|
public static final byte UDP_CMD_DATA = 0x03;
|
||||||
|
|
||||||
|
|
||||||
|
public static final byte TCP_CMD_ATTACH_CONFIG_FOUND = (byte) 0xE0;
|
||||||
|
public static final byte TCP_CMD_ATTACH_CONFIG_NOT_FOUND = (byte) 0xE1;
|
||||||
|
public static final byte TCP_CMD_ATTACH_USERDATA_OKAY = (byte) 0xE8;
|
||||||
|
public static final byte TCP_CMD_ATTACH_USERDATA_BAD = (byte) 0xE9;
|
||||||
|
|
||||||
|
private Protocol(){}
|
||||||
|
|
||||||
|
public enum HandshakeReturnCode {
|
||||||
|
BAD_HANDSHAKE,
|
||||||
|
SAME_CLIENT,
|
||||||
|
NEW_CLIENT
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] getRawAttachDataToSend(AttachCommand command) throws IOException {
|
||||||
|
return ByteBuffer.allocate(9)
|
||||||
|
.put(Protocol.TCP_CMD_ATTACH)
|
||||||
|
.putInt(command.getHandle())
|
||||||
|
.putShort(command.getVid())
|
||||||
|
.putShort(command.getPid())
|
||||||
|
.array();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] getRawDetachDataToSend(DetachCommand command) throws IOException {
|
||||||
|
return ByteBuffer.allocate(5)
|
||||||
|
.put(Protocol.TCP_CMD_DETACH)
|
||||||
|
.putInt(command.getHandle())
|
||||||
|
.array();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] getRawPingDataToSend(PingCommand command){
|
||||||
|
return new byte[]{Protocol.TCP_CMD_PING};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] getRawReadDataToSend(List<ReadCommand> readCommands) throws IOException {
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
|
DataOutputStream dos = new DataOutputStream(bos);
|
||||||
|
dos.writeByte(Protocol.UDP_CMD_DATA);
|
||||||
|
dos.writeByte(readCommands.size());
|
||||||
|
|
||||||
|
for(ReadCommand command : readCommands){
|
||||||
|
NetworkHIDDevice sender = command.getSender();
|
||||||
|
byte[] data = command.getData();
|
||||||
|
if(data.length > 0xFF){
|
||||||
|
log.info("Tried to send too much data. Maximum is 0xFF bytes read command.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte newLength = (byte)(data.length & 0xFF);
|
||||||
|
|
||||||
|
dos.writeInt(command.getHandle());
|
||||||
|
dos.writeShort(sender.getDeviceslot());
|
||||||
|
dos.writeByte(sender.getPadslot());
|
||||||
|
|
||||||
|
dos.write(newLength);
|
||||||
|
dos.write(data, 0, newLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
return bos.toByteArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,79 +26,96 @@ import java.io.DataOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.concurrent.SynchronousQueue;
|
||||||
|
|
||||||
import net.ash.HIDToVPADNetworkClient.controller.Controller;
|
import lombok.Getter;
|
||||||
import net.ash.HIDToVPADNetworkClient.controller.ControllerData;
|
import net.ash.HIDToVPADNetworkClient.network.Protocol.HandshakeReturnCode;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.util.HandleFoundry;
|
||||||
|
|
||||||
public class TCPClient {
|
public class TCPClient {
|
||||||
private Socket sock;
|
private Socket sock;
|
||||||
private DataInputStream in;
|
private DataInputStream in;
|
||||||
private DataOutputStream out;
|
private DataOutputStream out;
|
||||||
|
@Getter private int clientID = HandleFoundry.next();
|
||||||
|
|
||||||
|
private String ip;
|
||||||
|
|
||||||
public TCPClient() {
|
public TCPClient() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void connect(String ip) throws Exception {
|
public synchronized void connect(String ip) throws Exception {
|
||||||
|
this.ip = ip;
|
||||||
sock = new Socket();
|
sock = new Socket();
|
||||||
sock.connect(new InetSocketAddress(ip, Protocol.TCP_PORT), 2000);
|
sock.connect(new InetSocketAddress(ip, Protocol.TCP_PORT), 30000);
|
||||||
in = new DataInputStream(sock.getInputStream());
|
in = new DataInputStream(sock.getInputStream());
|
||||||
out = new DataOutputStream(sock.getOutputStream());
|
out = new DataOutputStream(sock.getOutputStream());
|
||||||
}
|
|
||||||
|
|
||||||
public enum HandshakeReturnCode {
|
|
||||||
BAD_HANDSHAKE,
|
|
||||||
SAME_CLIENT,
|
|
||||||
NEW_CLIENT
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized HandshakeReturnCode doHandshake() throws Exception {
|
|
||||||
if (in.readByte() != Protocol.TCP_HANDSHAKE) return HandshakeReturnCode.BAD_HANDSHAKE;
|
|
||||||
|
|
||||||
out.writeInt(NetworkManager.clientID);
|
if(doHandshake() == HandshakeReturnCode.BAD_HANDSHAKE){
|
||||||
out.flush();
|
throw new Exception();
|
||||||
System.out.println("[TCP] Handshaking...");
|
|
||||||
return (in.readByte() == Protocol.TCP_NEW_CLIENT) ? HandshakeReturnCode.NEW_CLIENT : HandshakeReturnCode.SAME_CLIENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void sendAttach(Controller c) throws Exception {
|
|
||||||
System.out.println("[TCPClient] Attach " + c); //XXX debug text
|
|
||||||
out.writeByte(Protocol.TCP_CMD_ATTACH);
|
|
||||||
|
|
||||||
|
|
||||||
out.writeInt(c.getHandle());
|
|
||||||
ControllerData d = c.getLatestData(); //GetLatestData allocates a new ControllerData
|
|
||||||
out.writeShort(d.getVID());
|
|
||||||
out.writeShort(d.getPID());
|
|
||||||
out.flush();
|
|
||||||
|
|
||||||
short deviceSlot = in.readShort();
|
|
||||||
byte padSlot = in.readByte();
|
|
||||||
c.setSlotData(deviceSlot, padSlot);
|
|
||||||
|
|
||||||
System.out.println("Attached! deviceSlot: " + Integer.toHexString((int)deviceSlot & 0xFFFF) + " padSlot: " + Integer.toHexString((int)padSlot & 0xFF));
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void sendDetach(Controller c) throws Exception {
|
|
||||||
System.out.println("[TCPClient] Detach " + c);
|
|
||||||
out.write(Protocol.TCP_CMD_DETACH);
|
|
||||||
|
|
||||||
out.writeInt(c.getHandle());
|
|
||||||
out.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized boolean ping() {
|
|
||||||
//System.out.println("Ping!");
|
|
||||||
try {
|
|
||||||
out.writeByte(Protocol.TCP_CMD_PING);
|
|
||||||
out.flush();
|
|
||||||
} catch (IOException e) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
//TODO convince Maschell to make the client actually respond to pings
|
}
|
||||||
|
|
||||||
|
private synchronized HandshakeReturnCode doHandshake() throws Exception {
|
||||||
|
if (recvByte() != Protocol.TCP_HANDSHAKE) return HandshakeReturnCode.BAD_HANDSHAKE;
|
||||||
|
|
||||||
|
send(clientID);
|
||||||
|
System.out.println("clientID:" + clientID);
|
||||||
|
|
||||||
|
System.out.println("[TCP] Handshaking...");
|
||||||
|
HandshakeReturnCode test = (recvByte() == Protocol.TCP_NEW_CLIENT) ? HandshakeReturnCode.NEW_CLIENT : HandshakeReturnCode.SAME_CLIENT;
|
||||||
|
System.out.println(test);
|
||||||
|
return test;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean abort(){
|
||||||
|
try {
|
||||||
|
sock.close();
|
||||||
|
clientID = HandleFoundry.next();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void abort() throws Exception {
|
public synchronized void send(byte[] rawCommand) throws IOException {
|
||||||
sock.close();
|
try{
|
||||||
}
|
out.write(rawCommand);
|
||||||
|
out.flush();
|
||||||
|
}catch(IOException e){
|
||||||
|
try {
|
||||||
|
connect(ip); //TODO: this is for reconnecting when the WiiU switches the application. But this breaks disconnecting, woops.
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void send(int value) throws IOException {
|
||||||
|
send(ByteBuffer.allocate(4).putInt(value).array());
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized byte recvByte() throws IOException {
|
||||||
|
try{
|
||||||
|
return in.readByte();
|
||||||
|
}catch(IOException e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized short recvShort() throws IOException {
|
||||||
|
try{
|
||||||
|
return in.readShort();
|
||||||
|
}catch(IOException e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean isConnected(){
|
||||||
|
return (sock != null && sock.isConnected() && !sock.isClosed());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,61 +21,33 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package net.ash.HIDToVPADNetworkClient.network;
|
package net.ash.HIDToVPADNetworkClient.network;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.HashMap;
|
import java.net.SocketException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
import net.ash.HIDToVPADNetworkClient.controller.Controller;
|
|
||||||
import net.ash.HIDToVPADNetworkClient.controller.ControllerData;
|
|
||||||
|
|
||||||
public class UDPClient {
|
public class UDPClient {
|
||||||
private DatagramSocket sock;
|
private final DatagramSocket sock;
|
||||||
private InetAddress host;
|
private final InetAddress host;
|
||||||
|
|
||||||
private ByteArrayOutputStream outBytes;
|
private UDPClient(String ip) throws SocketException, UnknownHostException{
|
||||||
private DataOutputStream out;
|
sock = new DatagramSocket();
|
||||||
|
host = InetAddress.getByName(ip);
|
||||||
public UDPClient() {
|
}
|
||||||
outBytes = new ByteArrayOutputStream();
|
public static UDPClient createUDPClient(String ip){
|
||||||
out = new DataOutputStream(outBytes);
|
UDPClient result = null;
|
||||||
|
try {
|
||||||
|
result = new UDPClient(ip);
|
||||||
|
} catch (Exception e) {
|
||||||
|
//handle?
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect(String ip) throws Exception {
|
public void send(byte[] data) throws IOException {
|
||||||
sock = new DatagramSocket();
|
DatagramPacket packet = new DatagramPacket(data, data.length, host, Protocol.UDP_PORT);
|
||||||
host = InetAddress.getByName(ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(HashMap<Integer, Controller> controllers) throws Exception {
|
|
||||||
out.writeByte(Protocol.UDP_CMD_DATA);
|
|
||||||
|
|
||||||
out.writeByte((byte)(controllers.size() & 0xFF));
|
|
||||||
|
|
||||||
for (Controller c : controllers.values()) {
|
|
||||||
out.writeInt(c.getHandle());
|
|
||||||
out.writeShort(c.getDeviceSlot());
|
|
||||||
out.writeByte(c.getPadSlot());
|
|
||||||
|
|
||||||
ControllerData d = c.getLatestData();
|
|
||||||
try {
|
|
||||||
out.writeInt(d.getData().length);
|
|
||||||
out.write(d.getData(), 0, d.getData().length);
|
|
||||||
} catch (NullPointerException e) {
|
|
||||||
out.writeInt(1);
|
|
||||||
out.writeByte(0x00);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
out.flush();
|
|
||||||
byte[] payload = outBytes.toByteArray();
|
|
||||||
DatagramPacket packet = new DatagramPacket(payload, payload.length, host, Protocol.UDP_PORT);
|
|
||||||
|
|
||||||
sock.send(packet);
|
sock.send(packet);
|
||||||
|
|
||||||
//System.out.println(Arrays.toString(payload)); //XXX debug text
|
|
||||||
|
|
||||||
outBytes.reset();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -19,41 +19,22 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package net.ash.HIDToVPADNetworkClient.gui;
|
package net.ash.HIDToVPADNetworkClient.network.commands;
|
||||||
|
import lombok.Getter;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.NetworkHIDDevice;
|
||||||
|
|
||||||
public class GuiController {
|
public class AttachCommand extends DeviceCommand {
|
||||||
private boolean activeState = false;
|
@Getter private final short vid;
|
||||||
private GuiControllerType type;
|
@Getter private final short pid;
|
||||||
private Object id;
|
|
||||||
|
public AttachCommand(int hidHandle, short vid, short pid, NetworkHIDDevice sender) {
|
||||||
public GuiController(GuiControllerType type, Object id) {
|
super(hidHandle,sender);
|
||||||
this.type = type;
|
this.vid = vid;
|
||||||
this.id = id;
|
this.pid = pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getId() {
|
@Override
|
||||||
return id;
|
public String toString() {
|
||||||
}
|
return "AttachCommand [vid=" + vid + ", pid=" + pid+ ", handle=" + getHandle() + ", sender=" + getSender() + "]";
|
||||||
|
}
|
||||||
public GuiControllerType getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean getActiveState() {
|
|
||||||
return activeState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setActiveState(boolean activeState) {
|
|
||||||
this.activeState = activeState;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "GuiController, active: " + activeState + ", type: " + type.toString() + ", id: " + id.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return id.hashCode() + type.hashCode() /*+ (activeState ? 1 : 0)*/;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
package net.ash.HIDToVPADNetworkClient.network.commands;
|
||||||
|
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.NetworkHIDDevice;
|
||||||
|
|
||||||
|
public class DetachCommand extends DeviceCommand{
|
||||||
|
public DetachCommand(int hidHandle, NetworkHIDDevice sender){
|
||||||
|
super(hidHandle,sender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "DetachCommand [handle=" + getHandle() + ", sender=" + getSender() + "]";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
package net.ash.HIDToVPADNetworkClient.network.commands;
|
||||||
|
import lombok.Data;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.NetworkHIDDevice;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public abstract class DeviceCommand {
|
||||||
|
private final int handle;
|
||||||
|
private final NetworkHIDDevice sender;
|
||||||
|
private final Class<? extends DeviceCommand> type;
|
||||||
|
|
||||||
|
protected DeviceCommand(int hidHandle,NetworkHIDDevice sender){
|
||||||
|
this.handle = hidHandle;
|
||||||
|
this.sender = sender;
|
||||||
|
this.type = this.getClass();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
package net.ash.HIDToVPADNetworkClient.network.commands;
|
||||||
|
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.NetworkHIDDevice;
|
||||||
|
|
||||||
|
public class PingCommand extends DeviceCommand {
|
||||||
|
public PingCommand() {
|
||||||
|
this((short) 0,null);
|
||||||
|
}
|
||||||
|
private PingCommand(int hidHandle, NetworkHIDDevice sender) {
|
||||||
|
super(hidHandle, sender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "PingCommand []";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
package net.ash.HIDToVPADNetworkClient.network.commands;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import net.ash.HIDToVPADNetworkClient.network.NetworkHIDDevice;
|
||||||
|
|
||||||
|
public class ReadCommand extends DeviceCommand{
|
||||||
|
@Getter private final byte[] data;
|
||||||
|
public ReadCommand(int hidHandle,byte[] data, NetworkHIDDevice sender) {
|
||||||
|
super(hidHandle, sender);
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ReadCommand [handle=" + getHandle() + ", sender=" + getSender() + "]";
|
||||||
|
}
|
||||||
|
}
|
@ -1,105 +0,0 @@
|
|||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*******************************************************************************/
|
|
||||||
package net.ash.HIDToVPADNetworkClient.util;
|
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
public class BlockingIOStabbifier extends InputStream implements Runnable {
|
|
||||||
private byte[] buffer;
|
|
||||||
private BufferedInputStream file;
|
|
||||||
private ByteArrayOutputStream baos;
|
|
||||||
private boolean running;
|
|
||||||
|
|
||||||
int baosSize = 0;
|
|
||||||
int bufferRemaining = 0;
|
|
||||||
int bufferOffset = -1;
|
|
||||||
|
|
||||||
public void run() {
|
|
||||||
while (running) {
|
|
||||||
try {
|
|
||||||
synchronized (baos) {
|
|
||||||
baos.write(file.read());
|
|
||||||
baosSize++;
|
|
||||||
}
|
|
||||||
Thread.sleep(1);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void allocate() {
|
|
||||||
synchronized (baos) {
|
|
||||||
buffer = baos.toByteArray();
|
|
||||||
bufferRemaining = baos.size();
|
|
||||||
bufferOffset = -1;
|
|
||||||
baos.reset();
|
|
||||||
baosSize = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int read() throws IOException {
|
|
||||||
if (bufferRemaining == 0) {
|
|
||||||
allocate();
|
|
||||||
if (bufferRemaining == 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bufferRemaining--;
|
|
||||||
bufferOffset++;
|
|
||||||
return buffer[bufferOffset] & 0xFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean markSupported() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException {
|
|
||||||
running = false;
|
|
||||||
file.close();
|
|
||||||
baos.close();
|
|
||||||
super.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int available() {
|
|
||||||
return bufferRemaining + baosSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BlockingIOStabbifier(String path) throws FileNotFoundException {
|
|
||||||
file = new BufferedInputStream(new FileInputStream(path));
|
|
||||||
|
|
||||||
baos = new ByteArrayOutputStream();
|
|
||||||
buffer = new byte[0];
|
|
||||||
|
|
||||||
running = true;
|
|
||||||
new Thread(this).start();
|
|
||||||
}
|
|
||||||
}
|
|
51
src/net/ash/HIDToVPADNetworkClient/util/HID4JavaManager.java
Normal file
51
src/net/ash/HIDToVPADNetworkClient/util/HID4JavaManager.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
package net.ash.HIDToVPADNetworkClient.util;
|
||||||
|
|
||||||
|
import org.hid4java.HidDevice;
|
||||||
|
import org.hid4java.HidManager;
|
||||||
|
import org.hid4java.HidServices;
|
||||||
|
|
||||||
|
public class HID4JavaManager {
|
||||||
|
|
||||||
|
private HID4JavaManager(){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches the corresponding HIDDevice for the given path
|
||||||
|
* @param path Path of the HIDDevice
|
||||||
|
* @return It the device is found, it will be returned. Otherwise null is returned.
|
||||||
|
*/
|
||||||
|
public static HidDevice getDeviceByPath(String path){
|
||||||
|
HidDevice result = null;
|
||||||
|
|
||||||
|
HidServices services = HidManager.getHidServices();
|
||||||
|
if(services == null) return result;
|
||||||
|
|
||||||
|
for (HidDevice device : services.getAttachedHidDevices()) {
|
||||||
|
if (device.getPath().equals(path)) {
|
||||||
|
result = device;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -25,10 +25,15 @@
|
|||||||
|
|
||||||
package net.ash.HIDToVPADNetworkClient.util;
|
package net.ash.HIDToVPADNetworkClient.util;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
public class HandleFoundry {
|
public class HandleFoundry {
|
||||||
private static int h = 1;
|
//We start with a random value, so we have at each startup a different clientID!
|
||||||
|
private static int h = ThreadLocalRandom.current().nextInt(1, 10000);
|
||||||
|
|
||||||
|
private HandleFoundry(){}
|
||||||
|
|
||||||
public static int next() {
|
public static int next() {
|
||||||
return h++;
|
return h++;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -21,32 +21,10 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package net.ash.HIDToVPADNetworkClient.util;
|
package net.ash.HIDToVPADNetworkClient.util;
|
||||||
|
|
||||||
public class WakeupThread extends Thread {
|
public class Settings {
|
||||||
private Object wakeup;
|
public static final int DETECT_CONTROLLER_INTERVAL = 1000;
|
||||||
private int timeout;
|
public static final int HANDLE_INPUTS_INTERVAL = 15;
|
||||||
private boolean run;
|
|
||||||
public WakeupThread(Object wakeup) {
|
private Settings(){}
|
||||||
super();
|
|
||||||
this.wakeup = wakeup;
|
|
||||||
run = true;
|
|
||||||
}
|
|
||||||
public void setTimeout(int timeout) {
|
|
||||||
this.timeout = timeout;
|
|
||||||
}
|
|
||||||
public void tryStop() {
|
|
||||||
run = false;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
while (run) {
|
|
||||||
synchronized (wakeup) {
|
|
||||||
wakeup.notify();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Thread.sleep(timeout);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
70
src/net/ash/HIDToVPADNetworkClient/util/Utilities.java
Normal file
70
src/net/ash/HIDToVPADNetworkClient/util/Utilities.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*******************************************************************************/
|
||||||
|
package net.ash.HIDToVPADNetworkClient.util;
|
||||||
|
|
||||||
|
public class Utilities{
|
||||||
|
|
||||||
|
private Utilities(){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Let me just sleep!
|
||||||
|
* @param ms sleep duration in ms
|
||||||
|
*/
|
||||||
|
public static void sleep(int ms) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(ms);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a byte array to a formated String
|
||||||
|
* @param ba byte array
|
||||||
|
* @return String representing the binary data
|
||||||
|
*/
|
||||||
|
public static String ByteArrayToString(byte[] ba){
|
||||||
|
if(ba == null) return null;
|
||||||
|
StringBuilder hex = new StringBuilder(ba.length * 2);
|
||||||
|
for(byte b : ba){
|
||||||
|
hex.append(String.format("%02X", b));
|
||||||
|
}
|
||||||
|
return hex.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a signed short value to a unsigned byte
|
||||||
|
* @param value short value
|
||||||
|
* @return converted value
|
||||||
|
*/
|
||||||
|
public static short signedShortToByte(int value){
|
||||||
|
return (short) (((((short)value) + Short.MAX_VALUE + 1) >> 8) & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a signed short value to a unsigned byte
|
||||||
|
* @param value short value
|
||||||
|
* @return converted value
|
||||||
|
*/
|
||||||
|
public static short signedShortToByte(short value){
|
||||||
|
return signedShortToByte((int) value);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user