Updated Gui, HidManager refactoring, documentation,logs

- Improved the GUI a bit
- Changed HidManagerBackend to be an interface instead of an abstract
class. The methods were moved to HidManager
- simple documentation of the HidDevice interface (realls need more
polishing though, very basic atm)
- added logging for the current platform and backend being used
This commit is contained in:
Maschell 2017-04-05 20:38:03 +02:00
parent 2eaab5a211
commit 857c5d397d
9 changed files with 97 additions and 61 deletions

View File

@ -15,4 +15,5 @@ Configuration files for HID to VPAD can be found [here](https://github.com/Masch
## Used Libraries ## Used Libraries
Lombok - https://projectlombok.org/index.html Lombok - https://projectlombok.org/index.html
purejavahidapi - https://github.com/nyholku/purejavahidapi purejavahidapi - https://github.com/nyholku/purejavahidapi
hid4java - https://github.com/gary-rowe/hid4java
JXInput - https://github.com/StrikerX3/JXInput JXInput - https://github.com/StrikerX3/JXInput

View File

@ -41,6 +41,8 @@ import lombok.Getter;
import net.ash.HIDToVPADNetworkClient.manager.ControllerManager; import net.ash.HIDToVPADNetworkClient.manager.ControllerManager;
import net.ash.HIDToVPADNetworkClient.network.NetworkManager; import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
import net.ash.HIDToVPADNetworkClient.util.Settings; import net.ash.HIDToVPADNetworkClient.util.Settings;
import java.awt.BorderLayout;
import java.awt.CardLayout;
public final class GuiInputControls extends JPanel { public final class GuiInputControls extends JPanel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -55,7 +57,7 @@ public final class GuiInputControls extends JPanel {
super(); super();
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setPreferredSize(new Dimension(220, 150)); setPreferredSize(new Dimension(220, 200));
final JButton connectButton = new JButton(CONNECT); final JButton connectButton = new JButton(CONNECT);
connectButton.setAlignmentX(Component.CENTER_ALIGNMENT); connectButton.setAlignmentX(Component.CENTER_ALIGNMENT);
@ -87,8 +89,9 @@ public final class GuiInputControls extends JPanel {
}); });
JPanel scanWrap = new JPanel(); JPanel scanWrap = new JPanel();
scanWrap.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); scanWrap.setLayout(new BoxLayout(scanWrap, BoxLayout.X_AXIS));
scanWrap.add(new JLabel("Auto Scan for Controllers: ")); JLabel label = new JLabel("Auto Scan for Controllers: ");
scanWrap.add(label);
scanWrap.add(cbautoScanForController); scanWrap.add(cbautoScanForController);
ipTextBox = new JTextField(); ipTextBox = new JTextField();
@ -111,10 +114,10 @@ public final class GuiInputControls extends JPanel {
} }
}); });
JPanel autoActivateWrap = new JPanel(new FlowLayout()); JPanel autoActivateWrap = new JPanel();
autoActivateWrap.setLayout(new BoxLayout(autoActivateWrap, BoxLayout.X_AXIS));
autoActivateWrap.add(new JLabel("Auto Activate Controller: ")); autoActivateWrap.add(new JLabel("Auto Activate Controller: "));
autoActivateWrap.add(cbautoActivateController); autoActivateWrap.add(cbautoActivateController);
autoActivateWrap.setMaximumSize(new Dimension(1000, 20));
add(Box.createVerticalGlue()); add(Box.createVerticalGlue());
@ -124,9 +127,12 @@ public final class GuiInputControls extends JPanel {
add(connectButton); add(connectButton);
add(Box.createRigidArea(new Dimension(1, 4))); add(Box.createRigidArea(new Dimension(1, 4)));
add(scanButton); add(scanButton);
add(Box.createRigidArea(new Dimension(1, 4)));
add(scanWrap); add(scanWrap);
add(Box.createVerticalGlue()); add(Box.createRigidArea(new Dimension(1, 4)));
add(autoActivateWrap); add(autoActivateWrap);
add(Box.createVerticalGlue());
add(Box.createVerticalGlue()); add(Box.createVerticalGlue());

View File

@ -21,20 +21,47 @@
*******************************************************************************/ *******************************************************************************/
package net.ash.HIDToVPADNetworkClient.hid; package net.ash.HIDToVPADNetworkClient.hid;
//TODO: Documentation...
public interface HidDevice { public interface HidDevice {
/**
* Opens the HidDevice for usage.
* @return true on success, false when it failed.
*/
boolean open();
/**
* Closes the HidDevice
*/
void close();
/**
* Returns the VendorID of the HidDevice
* @return vendorID
*/
short getVendorId(); short getVendorId();
/**
* Returns the ProductID of the HidDevice
* @return productID
*/
short getProductId(); short getProductId();
void close();
/**
* Returns the latest data.
* @return An byte array containing the latest data. If no data is present, it'll return a empty byte array
*/
byte[] getLatestData(); byte[] getLatestData();
/**
* Retuns the Usage of this HID-Device
* @return usage
*/
short getUsage(); short getUsage();
/**
* Returns the path of this HidDevice
* @return path
*/
String getPath(); String getPath();
boolean open();
} }

View File

@ -22,22 +22,57 @@
package net.ash.HIDToVPADNetworkClient.hid; package net.ash.HIDToVPADNetworkClient.hid;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import lombok.extern.java.Log;
import net.ash.HIDToVPADNetworkClient.hid.hid4java.Hid4JavaHidManagerBackend; import net.ash.HIDToVPADNetworkClient.hid.hid4java.Hid4JavaHidManagerBackend;
import net.ash.HIDToVPADNetworkClient.hid.purejavahid.PureJavaHidManagerBackend; import net.ash.HIDToVPADNetworkClient.hid.purejavahid.PureJavaHidManagerBackend;
import net.ash.HIDToVPADNetworkClient.util.Settings; import net.ash.HIDToVPADNetworkClient.util.Settings;
@Log
public class HidManager { public class HidManager {
private final static HidManagerBackend backend; private final static HidManagerBackend backend;
public static List<HidDevice> getAttachedController() {
return backend.getAttachedController();
}
public static HidDevice getDeviceByPath(String path) throws IOException { public static HidDevice getDeviceByPath(String path) throws IOException {
return backend.getDeviceByPath(path); return backend.getDeviceByPath(path);
} }
public static List<HidDevice> getAttachedControllers() {
List<HidDevice> connectedGamepads = new ArrayList<HidDevice>();
for (HidDevice info : backend.enumerateDevices()) {
if (isGamepad(info)) {
// Skip Xbox controller under windows. We should use XInput instead.
if (isXboxController(info) && Settings.isWindows()) {
continue;
}
connectedGamepads.add(info);
}
}
return connectedGamepads;
}
public static boolean isGamepad(HidDevice info) {
if (info == null) return false;
short usage = info.getUsage();
return (usage == 0x05 || usage == 0x04 || isNintendoController(info) || isPlaystationController(info));
}
private static boolean isPlaystationController(HidDevice info) {
if (info == null) return false;
return (info.getVendorId() == 0x054c);
}
private static boolean isNintendoController(HidDevice info) {
if (info == null) return false;
return (info.getVendorId() == 0x57e);
}
private static boolean isXboxController(HidDevice info) {
if (info == null) return false;
return (info.getVendorId() == 0x045e) && ((info.getProductId() == 0x02ff) || (info.getProductId() == 0x02a1));
}
static { static {
if (Settings.isMacOSX()) { if (Settings.isMacOSX()) {
@ -46,7 +81,14 @@ public class HidManager {
backend = new PureJavaHidManagerBackend(); backend = new PureJavaHidManagerBackend();
} else if (Settings.isLinux()) { } else if (Settings.isLinux()) {
backend = new Hid4JavaHidManagerBackend(); backend = new Hid4JavaHidManagerBackend();
} else } else{
backend = null; backend = null;
}
log.info("Plattform: " + System.getProperty("os.name"));
if(backend != null){
log.info("Backend: " + backend.getClass().getSimpleName());
}else{
log.info("No Backend loaded =(");
}
} }
} }

View File

@ -22,12 +22,9 @@
package net.ash.HIDToVPADNetworkClient.hid; package net.ash.HIDToVPADNetworkClient.hid;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.ash.HIDToVPADNetworkClient.util.Settings; public interface HidManagerBackend {
public abstract class HidManagerBackend {
/** /**
* Searches the corresponding HIDDevice for the given path * Searches the corresponding HIDDevice for the given path
* *
@ -38,41 +35,5 @@ public abstract class HidManagerBackend {
*/ */
public abstract HidDevice getDeviceByPath(String path) throws IOException; public abstract HidDevice getDeviceByPath(String path) throws IOException;
public List<HidDevice> getAttachedController() {
List<HidDevice> connectedGamepads = new ArrayList<HidDevice>();
for (HidDevice info : enumerateDevices()) {
if (isGamepad(info)) {
// Skip Xbox controller under windows. We should use XInput instead.
if (isXboxController(info) && Settings.isWindows()) {
continue;
}
connectedGamepads.add(info);
}
}
return connectedGamepads;
}
public static boolean isGamepad(HidDevice info) {
if (info == null) return false;
short usage = info.getUsage();
return (usage == 0x05 || usage == 0x04 || isNintendoController(info) || isPlaystationController(info));
}
private static boolean isPlaystationController(HidDevice info) {
if (info == null) return false;
return (info.getVendorId() == 0x054c);
}
private static boolean isNintendoController(HidDevice info) {
if (info == null) return false;
return (info.getVendorId() == 0x57e);
}
private static boolean isXboxController(HidDevice info) {
if (info == null) return false;
return (info.getVendorId() == 0x045e) && ((info.getProductId() == 0x02ff) || (info.getProductId() == 0x02a1));
}
public abstract List<HidDevice> enumerateDevices(); public abstract List<HidDevice> enumerateDevices();
} }

View File

@ -31,7 +31,7 @@ import org.hid4java.HidServices;
import net.ash.HIDToVPADNetworkClient.hid.HidDevice; import net.ash.HIDToVPADNetworkClient.hid.HidDevice;
import net.ash.HIDToVPADNetworkClient.hid.HidManagerBackend; import net.ash.HIDToVPADNetworkClient.hid.HidManagerBackend;
public class Hid4JavaHidManagerBackend extends HidManagerBackend { public class Hid4JavaHidManagerBackend implements HidManagerBackend {
@Override @Override
public HidDevice getDeviceByPath(String path) throws IOException { public HidDevice getDeviceByPath(String path) throws IOException {

View File

@ -34,7 +34,7 @@ class PureJavaHidDevice implements HidDevice, InputReportListener {
private final purejavahidapi.HidDeviceInfo myDeviceInfo; private final purejavahidapi.HidDeviceInfo myDeviceInfo;
private final Object dataLock = new Object(); private final Object dataLock = new Object();
protected byte[] currentData = new byte[1]; protected byte[] currentData = new byte[0];
public PureJavaHidDevice(HidDeviceInfo info) { public PureJavaHidDevice(HidDeviceInfo info) {
this.myDeviceInfo = info; this.myDeviceInfo = info;

View File

@ -29,8 +29,7 @@ import net.ash.HIDToVPADNetworkClient.hid.HidDevice;
import net.ash.HIDToVPADNetworkClient.hid.HidManagerBackend; import net.ash.HIDToVPADNetworkClient.hid.HidManagerBackend;
import purejavahidapi.PureJavaHidApi; import purejavahidapi.PureJavaHidApi;
public class PureJavaHidManagerBackend extends HidManagerBackend { public class PureJavaHidManagerBackend implements HidManagerBackend {
@Override @Override
public List<HidDevice> enumerateDevices() { public List<HidDevice> enumerateDevices() {
List<HidDevice> result = new ArrayList<HidDevice>(); List<HidDevice> result = new ArrayList<HidDevice>();

View File

@ -157,7 +157,7 @@ public final class ControllerManager {
private static Map<String, ControllerType> detectHIDDevices() { private static Map<String, ControllerType> detectHIDDevices() {
Map<String, ControllerType> connectedDevices = new HashMap<String, ControllerType>(); Map<String, ControllerType> connectedDevices = new HashMap<String, ControllerType>();
for (HidDevice info : HidManager.getAttachedController()) { for (HidDevice info : HidManager.getAttachedControllers()) {
String path = info.getPath(); String path = info.getPath();
connectedDevices.put(path, ControllerType.HIDController); connectedDevices.put(path, ControllerType.HIDController);
} }