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:
Maschell 2017-02-05 22:04:09 +01:00
parent 25207c85ca
commit 82a25e0cf4
35 changed files with 1789 additions and 1443 deletions

17
pom.xml
View File

@ -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>

View File

@ -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);

View File

@ -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);
/**
* Destroys the controller driver.
* <br>
* Will not return until all threads are stopped and resources are freed.
*/
public void destroy();
/** private void endThread() {
* Sets the deviceSlot and padSlot to be returned by {@link #getDeviceSlot() getDeviceSlot} and {@link #getPadSlot() getPadSlot}. new Thread(new Runnable() {
* @param deviceSlot Value to be returned by {@link #getDeviceSlot() getDeviceSlot} @Override
* @param padSlot Value to be returned by {@link #getPadSlot() getPadSlot} public void run() {
*/ setActive(false);
public void setSlotData(short deviceSlot, byte padSlot);
/** synchronized (shutdownLock) {
* Gets the previously set device slot (see {@link #setSlotData(short, byte) setSlotData}) shutdown = true;
* @return The controller's device slot. }
*/
public short getDeviceSlot();
/** boolean done = false;
* Gets the previously set pad slot (see {@link #setSlotData(short, byte) setSlotData}) int i = 0;
* @return The controller's pad slot. while(!done){
*/ synchronized (shutdownLock) {
public byte getPadSlot(); done = shutdownDone;
}
Utilities.sleep(50);
if(i++ > 50) System.out.println("Thread doesn't stop!!");
}
}
}).start();
}
/** public abstract short getVID();
* Returns a unique handle for this controller driver.
* <br>
* Please note that this is unique to the <i>driver</i>, not the controller it's connected to.
* @return The driver's handle.
*/
public int getHandle();
/** public abstract short getPID();
* Gets the controller's ID. This is often identical to the argument to {@link #initController(Object) initController}.
* @return The controller's ID. @Synchronized("shutdownLock")
*/ public boolean isActive() {
public String getID(); return active;
}
public void setActive(boolean active) {
this.active = active;
}
@Override
public String toString(){
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
}
} }

View File

@ -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;
}
}

View File

@ -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 String path = null; private static final int PACKET_LENGTH = 128;
private HidDevice controller = null;
@Override @Getter @Setter(AccessLevel.PRIVATE)
public boolean initController(Object arg) { private HidDevice hidDevice;
for (HidDevice device : HidManager.getHidServices().getAttachedHidDevices()) {
if (device.getPath().equals(arg.toString())) {
controller = device;
controller.setNonBlocking(true);
path = arg.toString(); @Override
public boolean initController(String identifier) {
break; HidDevice device = HID4JavaManager.getDeviceByPath(identifier);
} //device.setNonBlocking(true); //TODO: What does this do? This is done no in a extra Thread so it shouldn't matter.
} device.open();
Utilities.sleep(20); //What a bit until is opened.
System.out.println("ctrl: " + controller.open() + " " + controller.getLastErrorMessage()); if (device == null | !device.isOpen()) return false;
if (controller == null | !controller.isOpen()) return false; setHidDevice(device);
//System.out.println("ctrl: " + device.isOpen() + " " + device.getLastErrorMessage());
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
public void destroyDriver() {
getHidDevice().close();
} }
@Override @Override
public ControllerData getLatestData() { public short getVID() {
ControllerData data = new ControllerData(getVID(), getPID(), getHandle(), new byte[1200]); return getHidDevice().getVendorId();
System.out.println("Data size: " + controller.read(data.data, 500));
System.out.println("hrm: " + controller.getLastErrorMessage());
return data;
} }
@Override @Override
public void destroy() { public short getPID() {
controller.close(); return getHidDevice().getProductId();
}
private short getVID() {
return controller.getVendorId();
}
private short getPID() {
return controller.getProductId();
}
private short deviceSlot = 0;
private byte padSlot = 0;
@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;
}
private int handle = 0;
@Override
public int getHandle() {
if (handle == 0) {
handle = HandleFoundry.next();
}
return handle;
}
@Override
public String getID() {
return path;
} }
} }

View File

@ -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!");
e.printStackTrace();
return false;
}
processNextControllerEvent(); setVID((short)(identifier.hashCode() & 0xFFFF));
} setPID((short)((identifier.hashCode() >> Short.BYTES) & 0xFFFF));
System.out.println("[LinuxDevInputController] " + identifier.toString() + " fakevid: " + Integer.toHexString((int)getVID() & 0xFFFF) + " fakepid: " + Integer.toHexString((int)getPID() & 0xFFFF));
if (shutdown) break; return true;
/* Not polling. Wait for setPollingState to wake us up. */ }
try {
this.wait();
} catch (InterruptedException e) {}
} //for (;;)
}
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]");
} }
} }

View File

@ -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
}
}

View File

@ -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;
protected byte[] data;
public ControllerData(short vid, short pid, int handle, byte[] data) { public class XInput13Controller extends XInputController {
this.vid = vid; public XInput13Controller(String identifier) throws ControllerInitializationFailedException {
this.pid = pid; super(ControllerType.XINPUT13, identifier);
this.data = data; }
}
public ControllerData() {}
public short getVID() {
return vid;
}
public short getPID() {
return pid;
}
public byte[] getData() {
return data;
}
} }

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
} }

View File

@ -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()));
}
}
}

View File

@ -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) {}
} }

View File

@ -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());
}
}
return controllers; List<Controller> attachedControllers = ControllerManager.getAttachedControllers();
}
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!");
}
} }
} }
} }

View File

@ -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);
checkbox.setSelected(controller.isActive());
}
public GuiController getData() { @Override
return data; public int hashCode() {
} final int prime = 31;
int result = 1;
result = prime * result + ((controller == null) ? 0 : controller.hashCode());
return result;
}
@Override @Override
public int hashCode() { public boolean equals(Object obj) {
return data.hashCode(); 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;
}
} }

View File

@ -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());
}
}
});
}
} }

View File

@ -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;
}
}

View File

@ -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;
} }

View File

@ -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);
}
}
}
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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();
private UDPClient udpClient = null;
public static int clientID; private static NetworkManager instance = null;
private NetworkManager() {
private static NetworkManager instance = null; }
private Object controllersLock = new Object(); public static NetworkManager getInstance(){
private HashMap<Integer, Controller> controllers = new HashMap<Integer, Controller>(); if(instance == null){
instance = new NetworkManager();
}
return instance;
}
private Thread networkThread; @Getter private List<NetworkHIDDevice> devices = new ArrayList<>();
private TCPClient tcp; public void addHIDDevice(NetworkHIDDevice device){
private UDPClient udp; if(!getDevices().contains(device)){
synchronized (devices) {
getDevices().add(device);
}
}
}
private int packetInterval = 100; /*
* We want to remove them at the end of a cycle. To make sure the detach was send before removing.
*/
@Getter private List<NetworkHIDDevice> toRemove = new ArrayList<>();
@Synchronized("toRemove")
public void removeHIDDevice(NetworkHIDDevice device) {
device.sendDetach();
toRemove.add(device);
}
private enum NetworkState { @Override
DISCONNECTED, public void run() {
FRESH, //Connected, no handshake int i = 0;
CONNECTED while(true){
} proccessCommands();
private NetworkState networkState = NetworkState.DISCONNECTED; Utilities.sleep(10);
if(i++ > 1000/10){
ping();
i = 0;
}
}
}
public NetworkManager() throws Exception { private void ping() {
if (instance != null) { if(isConnected())sendingCommand(new PingCommand());
throw new Exception("NetworkManager already has an instance!"); }
}
instance = this;
networkThread = new Thread(this); public void proccessCommands(){
tcp = new TCPClient(); List<DeviceCommand> commands = new ArrayList<>();
udp = new UDPClient(); synchronized (toRemove) {
synchronized (devices) {
for(NetworkHIDDevice device : getDevices()){
commands.addAll(device.getCommandList());
}
}
}
pingThread.start(); if(commands.isEmpty())return;
clientID = ThreadLocalRandom.current().nextInt(); //Split up into "read commands" and other commands.
System.out.println("[NetworkManager] clientID: " + clientID); List<ReadCommand> readCommands = new ArrayList<>();
} {
for(DeviceCommand command : commands){
if(command instanceof ReadCommand){
readCommands.add((ReadCommand) command);
}
}
commands.removeAll(readCommands);
}
private int runLoopCounter = 0; if(!readCommands.isEmpty()){
@Override sendingRead(readCommands);
public void run() { }
for (;;) {
for (;;) {
/*
* Socket is connected, handshake needed
*/
if (networkState == NetworkState.FRESH) {
try {
switch (tcp.doHandshake()) {
case BAD_HANDSHAKE: if(!commands.isEmpty()){
tcp.abort(); for(DeviceCommand command : commands){
networkState = NetworkState.DISCONNECTED; sendingCommand(command);
continue; }
}
case NEW_CLIENT: synchronized (toRemove) {
synchronized (controllersLock) { synchronized (devices) {
for (Controller c : controllers.values()) { for(NetworkHIDDevice d: toRemove){
tcp.sendAttach(c); commands.remove(d);
} }
} }
networkState = NetworkState.CONNECTED; }
break; }
case SAME_CLIENT: private void sendingCommand(DeviceCommand command) {
networkState = NetworkState.CONNECTED; if(command instanceof AttachCommand){
break; sendAttach((AttachCommand) command);
}else if(command instanceof DetachCommand){
sendDetach((DetachCommand) command);
}else if(command instanceof PingCommand){
sendPing((PingCommand) command);
}
}
default: private void sendPing(PingCommand command) {
tcp.abort(); if(sendTCP(Protocol.getRawPingDataToSend(command))){
networkState = NetworkState.DISCONNECTED; log.info("PING");
continue; }else{
log.info("Sending the PING failed");
}
}
} private void sendDetach(DetachCommand command) {
} catch (Exception e) { byte[] sendData;
e.printStackTrace(); try {
} sendData = Protocol.getRawDetachDataToSend(command);
} else if (networkState == NetworkState.CONNECTED) { if(!sendTCP(sendData)){
synchronized (controllersLock) { log.info("Sending the detach command for device (" + command.getSender() + ") failed. sendTCP failed");
try { }
udp.send(controllers); } catch (IOException e) {
} catch (Exception e) { e.printStackTrace();
e.printStackTrace(); }
}
}
if (runLoopCounter++ == PING_INTERVAL / packetInterval) { }
synchronized(pingThread) {
pingThread.notify();
}
runLoopCounter = 0;
}
sleep(packetInterval); private void sendAttach(AttachCommand command) {
} else if (networkState == NetworkState.DISCONNECTED) break; //Send the TCP command
} //for (;;) byte[] sendData = null;
try {
sendData = Protocol.getRawAttachDataToSend(command);
}catch (IOException e) {
log.info("Building the attach command for device (" + command.getSender() + ") failed." + e.getMessage());
return;
}
if(sendTCP(sendData)){
byte configFound = 0;
try {
configFound = recvTCPByte();
} catch (IOException e1) {
e1.printStackTrace();
}
if(configFound == Protocol.TCP_CMD_ATTACH_CONFIG_FOUND){
log.info("Config on the console found!");
}else if(configFound == Protocol.TCP_CMD_ATTACH_CONFIG_NOT_FOUND){
log.info("NO CONFIG FOUND.");
return;
}else if (configFound == 0){
log.info("Failed to get byte.");
disconnect();
}
try { byte userDataOkay = 0;
synchronized (networkThread) { try {
networkThread.wait(); userDataOkay = recvTCPByte();
} } catch (IOException e1) {
} catch (InterruptedException e) {} e1.printStackTrace();
} }
} if(userDataOkay == Protocol.TCP_CMD_ATTACH_USERDATA_OKAY){
log.info("userdata okay!");
}else if(userDataOkay == Protocol.TCP_CMD_ATTACH_USERDATA_BAD){
log.info("USERDATA BAD.");
return;
}else if (userDataOkay == 0){
log.info("Failed to get byte.");
disconnect();
}
public void connect(String ip) { //We receive our device slot and pad slot
System.out.println("[NetworkManager] Connecting to " + ip + "..."); //XXX debug text short deviceslot = -1;
try { byte padslot = -1;
udp.connect(ip); try {
tcp.connect(ip); deviceslot = recvTCPShort();
} catch (Exception e) { padslot = recvTCPByte();
System.err.println("[NetworkManager] Couldn't connect to Wii U!"); } catch (IOException e) {
e.printStackTrace(); log.info("Recieving data after sending a attach failed for device (" + command.getSender() + ") failed." + e.getMessage());
return; }
}
networkState = NetworkState.FRESH;
if (networkThread.getState() == Thread.State.NEW) {
networkThread.start();
} else if (networkThread.getState() == Thread.State.WAITING) {
synchronized (networkThread) {
networkThread.notify();
}
}
}
public void disconnect() { if(deviceslot < 0 || padslot < 0){
networkState = NetworkState.DISCONNECTED; log.info("Recieving data after sending a attach failed for device (" + command.getSender() + ") failed. We need to disconnect =(");
disconnect();
}
if (!Thread.currentThread().equals(networkThread) && networkThread.getState() != Thread.State.NEW) { //Let's save them for later.
while (networkThread.getState() != Thread.State.WAITING) {sleep(1);} NetworkHIDDevice sender = command.getSender();
} if(sender != null){
sender.setDeviceslot(deviceslot);
sender.setPadslot(padslot);
}else{
log.info("Something really went wrong. Got an attach event with out an " + NetworkHIDDevice.class.getSimpleName());
}
log.info("Attaching done!");
}else{
log.info("Sending the attach command for device (" + command.getSender() + ") failed. sendTCP failed");
}
}
try { private void sendingRead(List<ReadCommand> readCommands) {
tcp.abort(); byte[] rawCommand;
} catch (Exception e) { try {
e.printStackTrace(); rawCommand = Protocol.getRawReadDataToSend(readCommands);
} System.out.println("UDP Packet: "+ Utilities.ByteArrayToString(rawCommand));
sendUDP(rawCommand);
} catch (IOException e) {
System.out.println("Sending read data failed.");
}
}
System.out.println("[NetworkManager] Disconnected."); private boolean sendUDP(byte[] rawCommand) {
} boolean result = false;
if(udpClient != null){
try {
udpClient.send(rawCommand);
result = true;
} catch (Exception e) {
//
result = false;
}
}
return result;
}
/* private boolean sendTCP(byte[] rawCommand) {
* Is there an active connection? boolean result = false;
*/ if(tcpClient != null){
public boolean isConnected() { try {
return networkState == NetworkState.FRESH || networkState == NetworkState.CONNECTED; tcpClient.send(rawCommand);
} result = true;
} catch (Exception e) {
//
result = false;
}
}
return result;
}
/* public void disconnect() {
* Is the active connection good for data? ControllerManager.deactivateAllAttachedControllers();
*/ tcpClient.abort();
public boolean isRunning() { }
return networkState == NetworkState.CONNECTED;
}
public HashMap<Integer,Controller> getControllers() { private short recvTCPShort() throws IOException {
return controllers; return tcpClient.recvShort();
} }
public void setPacketInterval(int packetInterval) { private byte recvTCPByte() throws IOException {
this.packetInterval = packetInterval; return tcpClient.recvByte();
} }
public void addController(Controller controller) {
synchronized (controllersLock) {
if (isRunning()) {
try {
tcp.sendAttach(controller);
} catch (Exception e) {return;};
}
controllers.put(controller.getID().hashCode(), controller);
}
}
public void removeController(Controller controller) { public boolean isConnected() {
synchronized (controllersLock) { return (tcpClient != null && tcpClient.isConnected());
if (isRunning()) { }
try {
tcp.sendDetach(controller);
} catch (Exception e) {return;};
}
controller.destroy();
controllers.remove(controller.getID().hashCode());
}
}
public void removeAllControllers() { public boolean connect(String ip) {
synchronized (controllersLock) { boolean result = false;
for (Controller c : controllers.values()) { log.info("Trying to connect to: " + ip);
if (isRunning()) { try {
try { tcpClient.connect(ip);
tcp.sendDetach(c); System.out.println("TCP Connected!");
} catch (Exception e) {continue;}; udpClient = UDPClient.createUDPClient(ip);
} if(udpClient != null){
c.destroy(); result = true;
controllers.remove(c.getID().hashCode()); }
} } catch (Exception e) {
} System.out.println("Error while connecting: " + e.getMessage());
} }
return result;
private Thread pingThread = new Thread(new Runnable() { }
public void run() {
for (;;) {
synchronized (pingThread) {
try {
pingThread.wait();
} catch (InterruptedException e) {}
}
if (!tcp.ping()) {
System.out.println("[NetworkManager] Ping failed, disconnecting...");
GuiInteractionManager.instance().disconnect();
}
}
}
}, "Ping Thread");
private void sleep(long ticks) {
try {
Thread.sleep(ticks);
} catch (InterruptedException e) {}
}
public static NetworkManager instance() {
return instance;
}
} }

View File

@ -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();
}
} }

View File

@ -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 { if(doHandshake() == HandshakeReturnCode.BAD_HANDSHAKE){
BAD_HANDSHAKE, throw new Exception();
SAME_CLIENT,
NEW_CLIENT
}
public synchronized HandshakeReturnCode doHandshake() throws Exception {
if (in.readByte() != Protocol.TCP_HANDSHAKE) return HandshakeReturnCode.BAD_HANDSHAKE;
out.writeInt(NetworkManager.clientID);
out.flush();
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());
}
} }

View File

@ -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();
} }
} }

View File

@ -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 GuiController(GuiControllerType type, Object id) { public AttachCommand(int hidHandle, short vid, short pid, NetworkHIDDevice sender) {
this.type = type; super(hidHandle,sender);
this.id = id; this.vid = vid;
} 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)*/;
}
} }

View File

@ -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() + "]";
}
}

View File

@ -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();
}
}

View File

@ -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 []";
}
}

View File

@ -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() + "]";
}
}

View File

@ -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();
}
}

View 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;
}
}

View File

@ -25,8 +25,13 @@
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++;

View File

@ -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();
}
}
}
} }

View 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);
}
}