From e71d760cde9eab8942ac59d82780f752daa02ce2 Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 5 Apr 2017 11:44:21 +0200 Subject: [PATCH] Added an abstract HID-Layer, this way we can change the actual HID-Backend easily Currently Winows still uses purejavahidapi and Unix uses hid4java. This is currently untested. --- pom.xml | 9 ++- .../controller/Controller.java | 9 ++- .../controller/DS4NewController.java | 12 ++-- ...aHidController.java => HidController.java} | 50 ++++---------- .../controller/SwitchProController.java | 7 +- .../HIDToVPADNetworkClient/hid/HidDevice.java | 34 ++++++++++ .../hid/HidDeviceInfo.java | 33 +++++++++ .../hid/HidManager.java | 52 ++++++++++++++ .../HidManagerBackend.java} | 34 +++------- .../hid/hid4java/Hid4JavaHidDevice.java | 58 ++++++++++++++++ .../hid/hid4java/Hid4JavaHidDeviceInfo.java | 54 +++++++++++++++ .../hid4java/Hid4JavaHidManagerBackend.java | 61 +++++++++++++++++ .../hid/purejavahid/PureJavaHidDevice.java | 68 +++++++++++++++++++ .../purejavahid/PureJavaHidDeviceInfo.java | 53 +++++++++++++++ .../PureJavaHidManagerBackend.java | 57 ++++++++++++++++ .../manager/ControllerManager.java | 15 ++-- 16 files changed, 523 insertions(+), 83 deletions(-) rename src/net/ash/HIDToVPADNetworkClient/controller/{PureJavaHidController.java => HidController.java} (69%) create mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java create mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/HidDeviceInfo.java create mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/HidManager.java rename src/net/ash/HIDToVPADNetworkClient/{util/PureJavaHidApiManager.java => hid/HidManagerBackend.java} (72%) create mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java create mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDeviceInfo.java create mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidManagerBackend.java create mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java create mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDeviceInfo.java create mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidManagerBackend.java diff --git a/pom.xml b/pom.xml index 8a9a7c0..5a1aa59 100644 --- a/pom.xml +++ b/pom.xml @@ -138,9 +138,14 @@ 1eb4087 - com.github.Maschell + com.github.QuarkTheAwesome purejavahidapi - cbf0588 + 3591b7e + + + org.hid4java + hid4java + 0.4.0 diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java index 4792971..0f49235 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java @@ -21,6 +21,8 @@ *******************************************************************************/ package net.ash.HIDToVPADNetworkClient.controller; +import java.util.Arrays; + import lombok.Getter; import lombok.Synchronized; import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException; @@ -41,6 +43,8 @@ public abstract class Controller implements Runnable { @Getter private final String identifier; private byte[] latestData = null; + protected int MAX_PACKET_LENGTH = 64; + boolean shutdown = false; boolean shutdownDone = false; private final Object dataLock = new Object(); @@ -66,6 +70,9 @@ public abstract class Controller implements Runnable { while (isActive()) { byte[] newData = pollLatestData(); if (newData != null && newData.length != 0) { + if (newData.length > MAX_PACKET_LENGTH) { + newData = Arrays.copyOfRange(newData, 0, MAX_PACKET_LENGTH); + } setLatestData(newData); } doSleepAfterPollingData(); @@ -202,7 +209,7 @@ public abstract class Controller implements Runnable { } public enum ControllerType { - PureJAVAHid, LINUX, XINPUT13, XINPUT14 + HIDController, LINUX, XINPUT13, XINPUT14 } public abstract String getInfoText(); diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/DS4NewController.java b/src/net/ash/HIDToVPADNetworkClient/controller/DS4NewController.java index 22b1f8c..b9e2a78 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/DS4NewController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/DS4NewController.java @@ -5,26 +5,26 @@ import java.util.Arrays; import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException; import net.ash.HIDToVPADNetworkClient.util.Settings; -public class DS4NewController extends PureJavaHidController { +public class DS4NewController extends HidController { public static final short DS4_NEW_CONTROLLER_VID = 0x54C; public static final short DS4_NEW_CONTROLLER_PID = 0x09CC; public DS4NewController(String identifier) throws ControllerInitializationFailedException { super(identifier); if (Settings.isMacOSX()) { - this.PACKET_LENGTH = 7; + this.MAX_PACKET_LENGTH = 7; } else { - this.PACKET_LENGTH = 6; + this.MAX_PACKET_LENGTH = 6; } } @Override public byte[] pollLatestData() { + byte[] currentData = super.getLatestData(); if (Settings.isMacOSX()) { // for some reason the controller has one extra byte at the beginning under OSX - return Arrays.copyOfRange(currentData, 1, 7); + currentData = Arrays.copyOfRange(currentData, 1, 7); } - - return currentData.clone(); + return currentData; } @Override diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java b/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java similarity index 69% rename from src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java rename to src/net/ash/HIDToVPADNetworkClient/controller/HidController.java index 57c2523..662c7d1 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/PureJavaHidController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java @@ -22,63 +22,53 @@ package net.ash.HIDToVPADNetworkClient.controller; import java.io.IOException; -import java.util.Arrays; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; -import lombok.Synchronized; import lombok.extern.java.Log; import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException; -import net.ash.HIDToVPADNetworkClient.util.PureJavaHidApiManager; -import purejavahidapi.HidDevice; -import purejavahidapi.InputReportListener; +import net.ash.HIDToVPADNetworkClient.hid.HidDevice; +import net.ash.HIDToVPADNetworkClient.hid.HidManager; @Log -public class PureJavaHidController extends Controller implements InputReportListener { - private final Object dataLock = new Object(); - protected byte[] currentData = new byte[1]; - - protected int PACKET_LENGTH = 64; - +public class HidController extends Controller { @Getter @Setter(AccessLevel.PRIVATE) private HidDevice hidDevice; public static Controller getInstance(String deviceIdentifier) throws IOException, ControllerInitializationFailedException { - HidDevice device = PureJavaHidApiManager.getDeviceByPath(deviceIdentifier); + HidDevice device = HidManager.getDeviceByPath(deviceIdentifier); short vid = 0; short pid = 0; if (device != null) { - vid = device.getHidDeviceInfo().getVendorId(); - pid = device.getHidDeviceInfo().getProductId(); + vid = device.getVendorId(); + pid = device.getProductId(); device.close(); } // We use a special version to optimize the data for the switch pro controller if (vid == SwitchProController.SWITCH_PRO_CONTROLLER_VID && pid == SwitchProController.SWITCH_PRO_CONTROLLER_PID) { - return new SwitchProController(deviceIdentifier); } else if (vid == DS4NewController.DS4_NEW_CONTROLLER_VID && pid == DS4NewController.DS4_NEW_CONTROLLER_PID) { return new DS4NewController(deviceIdentifier); } else { - return new PureJavaHidController(deviceIdentifier); + return new HidController(deviceIdentifier); } } - public PureJavaHidController(String identifier) throws ControllerInitializationFailedException { - super(ControllerType.PureJAVAHid, identifier); + public HidController(String identifier) throws ControllerInitializationFailedException { + super(ControllerType.HIDController, identifier); } @Override public boolean initController(String identifier) { HidDevice device; try { - device = PureJavaHidApiManager.getDeviceByPath(identifier); + device = HidManager.getDeviceByPath(identifier); if (device == null) { return false; } - device.setInputReportListener(this); setHidDevice(device); return true; @@ -89,9 +79,8 @@ public class PureJavaHidController extends Controller implements InputReportList } @Override - @Synchronized("dataLock") public byte[] pollLatestData() { - return currentData.clone(); + return hidDevice.getLatestData(); } @Override @@ -105,29 +94,16 @@ public class PureJavaHidController extends Controller implements InputReportList throw e; } } - } @Override public short getVID() { - return getHidDevice().getHidDeviceInfo().getVendorId(); + return getHidDevice().getVendorId(); } @Override public short getPID() { - return getHidDevice().getHidDeviceInfo().getProductId(); - } - - @Override - @Synchronized("dataLock") - public void onInputReport(HidDevice source, byte reportID, byte[] reportData, int reportLength) { - if (isActive()) { - int length = PACKET_LENGTH; - if (reportLength < length) { - length = reportLength; - } - currentData = Arrays.copyOfRange(reportData, 0, length); - } + return getHidDevice().getProductId(); } @Override diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/SwitchProController.java b/src/net/ash/HIDToVPADNetworkClient/controller/SwitchProController.java index d1a6842..bc606b7 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/SwitchProController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/SwitchProController.java @@ -23,18 +23,19 @@ package net.ash.HIDToVPADNetworkClient.controller; import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException; -public class SwitchProController extends PureJavaHidController { +public class SwitchProController extends HidController { public static final short SWITCH_PRO_CONTROLLER_VID = 0x57e; public static final short SWITCH_PRO_CONTROLLER_PID = 0x2009; public SwitchProController(String identifier) throws ControllerInitializationFailedException { super(identifier); // truncate package to 11; - this.PACKET_LENGTH = 11; + this.MAX_PACKET_LENGTH = 11; } @Override public byte[] pollLatestData() { + byte[] currentData = super.pollLatestData(); if (currentData == null || currentData.length < 10) { return new byte[0]; } @@ -43,7 +44,7 @@ public class SwitchProController extends PureJavaHidController { currentData[5] = 0; currentData[7] = 0; currentData[9] = 0; - return currentData.clone(); + return currentData; } @Override diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java b/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java new file mode 100644 index 0000000..47a6b06 --- /dev/null +++ b/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * 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.hid; + +public interface HidDevice { + + short getVendorId(); + + short getProductId(); + + void close(); + + byte[] getLatestData(); + +} diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/HidDeviceInfo.java b/src/net/ash/HIDToVPADNetworkClient/hid/HidDeviceInfo.java new file mode 100644 index 0000000..ce7bcd4 --- /dev/null +++ b/src/net/ash/HIDToVPADNetworkClient/hid/HidDeviceInfo.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * 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.hid; + +public interface HidDeviceInfo { + + public short getUsagePage(); + + public int getVendorId(); + + public int getProductId(); + + public String getPath(); +} diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/HidManager.java b/src/net/ash/HIDToVPADNetworkClient/hid/HidManager.java new file mode 100644 index 0000000..4b0330e --- /dev/null +++ b/src/net/ash/HIDToVPADNetworkClient/hid/HidManager.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * 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.hid; + +import java.io.IOException; +import java.util.List; + +import net.ash.HIDToVPADNetworkClient.hid.hid4java.Hid4JavaHidManagerBackend; +import net.ash.HIDToVPADNetworkClient.hid.purejavahid.PureJavaHidManagerBackend; +import net.ash.HIDToVPADNetworkClient.util.Settings; + +public class HidManager { + private final static HidManagerBackend backend; + + public static List getAttachedController() { + return backend.getAttachedController(); + } + + public static HidDevice getDeviceByPath(String path) throws IOException { + return backend.getDeviceByPath(path); + } + + static { + if (Settings.isMacOSX()) { + backend = new Hid4JavaHidManagerBackend(); + } else if (Settings.isWindows()) { + backend = new PureJavaHidManagerBackend(); + } else if (Settings.isLinux()) { + backend = new Hid4JavaHidManagerBackend(); + } else + backend = null; + } +} diff --git a/src/net/ash/HIDToVPADNetworkClient/util/PureJavaHidApiManager.java b/src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java similarity index 72% rename from src/net/ash/HIDToVPADNetworkClient/util/PureJavaHidApiManager.java rename to src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java index 78fad0e..2ee7e35 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/PureJavaHidApiManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java @@ -19,22 +19,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *******************************************************************************/ -package net.ash.HIDToVPADNetworkClient.util; +package net.ash.HIDToVPADNetworkClient.hid; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import net.ash.HIDToVPADNetworkClient.manager.ControllerManager; -import purejavahidapi.HidDevice; -import purejavahidapi.HidDeviceInfo; -import purejavahidapi.PureJavaHidApi; - -public final class PureJavaHidApiManager { - - private PureJavaHidApiManager() { - } +import net.ash.HIDToVPADNetworkClient.util.Settings; +public abstract class HidManagerBackend { /** * Searches the corresponding HIDDevice for the given path * @@ -43,25 +36,12 @@ public final class PureJavaHidApiManager { * @return It the device is found, it will be returned. Otherwise null is returned. * @throws IOException */ - public static HidDevice getDeviceByPath(String path) throws IOException { - HidDeviceInfo deviceinfo = ControllerManager.getDeviceInfoByPath(path); - if (deviceinfo != null) { - HidDevice result = PureJavaHidApi.openDevice(deviceinfo); - if (result != null) { - return result; - } - } - /* - * List devList = PureJavaHidApi.enumerateDevices(); HidDevice result = null; for (HidDeviceInfo info : devList) { String real_path = - * info.getPath(); if (real_path.equals(path)) { return PureJavaHidApi.openDevice(info); } } - */ - return null; - } + public abstract HidDevice getDeviceByPath(String path) throws IOException; - public static List getAttachedController() { + public List getAttachedController() { List connectedGamepads = new ArrayList(); - for (HidDeviceInfo info : PureJavaHidApi.enumerateDevices()) { + for (HidDeviceInfo info : enumerateDevices()) { if (isGamepad(info)) { // Skip Xbox controller under windows. We should use XInput instead. if (isXboxController(info) && Settings.isWindows()) { @@ -93,4 +73,6 @@ public final class PureJavaHidApiManager { if (info == null) return false; return (info.getVendorId() == 0x045e) && ((info.getProductId() == 0x02ff) || (info.getProductId() == 0x02a1)); } + + public abstract List enumerateDevices(); } diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java new file mode 100644 index 0000000..197bbc0 --- /dev/null +++ b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * 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.hid.hid4java; + +import java.util.Arrays; + +import net.ash.HIDToVPADNetworkClient.hid.HidDevice; + +class Hid4JavaHidDevice implements HidDevice { + private final org.hid4java.HidDevice myDevice; + + private final byte[] data = new byte[64]; + + public Hid4JavaHidDevice(org.hid4java.HidDevice device) { + this.myDevice = device; + } + + @Override + public short getVendorId() { + return myDevice.getVendorId(); + } + + @Override + public short getProductId() { + return myDevice.getProductId(); + } + + @Override + public void close() { + myDevice.close(); + } + + @Override + public byte[] getLatestData() { + int length = myDevice.read(data); + if (length <= 0) return null; + return Arrays.copyOf(data, length); + } +} diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDeviceInfo.java b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDeviceInfo.java new file mode 100644 index 0000000..8ca5d24 --- /dev/null +++ b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDeviceInfo.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * 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.hid.hid4java; + +import org.hid4java.HidDevice; + +import net.ash.HIDToVPADNetworkClient.hid.HidDeviceInfo; + +class Hid4JavaHidDeviceInfo implements HidDeviceInfo { + private final HidDevice myDevice; + + public Hid4JavaHidDeviceInfo(HidDevice device) { + myDevice = device; + } + + @Override + public short getUsagePage() { + return (short) myDevice.getUsagePage(); + } + + @Override + public int getVendorId() { + return myDevice.getVendorId(); + } + + @Override + public int getProductId() { + return myDevice.getProductId(); + } + + @Override + public String getPath() { + return myDevice.getPath(); + } +} diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidManagerBackend.java b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidManagerBackend.java new file mode 100644 index 0000000..beabb21 --- /dev/null +++ b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidManagerBackend.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * 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.hid.hid4java; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.hid4java.HidManager; +import org.hid4java.HidServices; + +import net.ash.HIDToVPADNetworkClient.hid.HidDevice; +import net.ash.HIDToVPADNetworkClient.hid.HidDeviceInfo; +import net.ash.HIDToVPADNetworkClient.hid.HidManagerBackend; + +public class Hid4JavaHidManagerBackend extends HidManagerBackend { + + @Override + public HidDevice getDeviceByPath(String path) throws IOException { + HidDevice result = null; + HidServices services = HidManager.getHidServices(); + if (services == null) return result; + + for (org.hid4java.HidDevice device : services.getAttachedHidDevices()) { + if (device.getPath().equals(path)) { + result = new Hid4JavaHidDevice(device); + break; + } + } + return result; + } + + @Override + public List enumerateDevices() { + List result = new ArrayList(); + for (org.hid4java.HidDevice info : HidManager.getHidServices().getAttachedHidDevices()) { + result.add(new Hid4JavaHidDeviceInfo(info)); + } + return result; + } + +} diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java new file mode 100644 index 0000000..f1f614e --- /dev/null +++ b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * 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.hid.purejavahid; + +import java.util.Arrays; + +import lombok.Synchronized; +import net.ash.HIDToVPADNetworkClient.hid.HidDevice; +import purejavahidapi.InputReportListener; + +class PureJavaHidDevice implements HidDevice, InputReportListener { + private final purejavahidapi.HidDevice myDevice; + + private final Object dataLock = new Object(); + protected byte[] currentData = new byte[1]; + + public PureJavaHidDevice(purejavahidapi.HidDevice device) { + this.myDevice = device; + device.setInputReportListener(this); + } + + @Override + @Synchronized("dataLock") + public void onInputReport(purejavahidapi.HidDevice source, byte reportID, byte[] reportData, int reportLength) { + currentData = Arrays.copyOfRange(reportData, 0, reportLength); + } + + @Override + public short getVendorId() { + return myDevice.getHidDeviceInfo().getVendorId(); + } + + @Override + public short getProductId() { + return myDevice.getHidDeviceInfo().getProductId(); + } + + @Override + public void close() { + myDevice.close(); + } + + @Override + @Synchronized("dataLock") + public byte[] getLatestData() { + return currentData.clone(); + } + +} diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDeviceInfo.java b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDeviceInfo.java new file mode 100644 index 0000000..f65c221 --- /dev/null +++ b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDeviceInfo.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * 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.hid.purejavahid; + +import net.ash.HIDToVPADNetworkClient.hid.HidDeviceInfo; + +class PureJavaHidDeviceInfo implements HidDeviceInfo { + private final purejavahidapi.HidDeviceInfo myInfo; + + public PureJavaHidDeviceInfo(purejavahidapi.HidDeviceInfo info) { + this.myInfo = info; + } + + @Override + public short getUsagePage() { + return myInfo.getUsagePage(); + } + + @Override + public int getVendorId() { + return myInfo.getVendorId(); + } + + @Override + public int getProductId() { + return myInfo.getProductId(); + } + + @Override + public String getPath() { + return myInfo.getPath(); + } + +} diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidManagerBackend.java b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidManagerBackend.java new file mode 100644 index 0000000..3d7c90a --- /dev/null +++ b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidManagerBackend.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * 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.hid.purejavahid; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import net.ash.HIDToVPADNetworkClient.hid.HidDevice; +import net.ash.HIDToVPADNetworkClient.hid.HidDeviceInfo; +import net.ash.HIDToVPADNetworkClient.hid.HidManagerBackend; +import purejavahidapi.PureJavaHidApi; + +public class PureJavaHidManagerBackend extends HidManagerBackend { + + @Override + public List enumerateDevices() { + List result = new ArrayList(); + for (purejavahidapi.HidDeviceInfo info : PureJavaHidApi.enumerateDevices()) { + result.add(new PureJavaHidDeviceInfo(info)); + } + return result; + } + + @Override + public HidDevice getDeviceByPath(String path) throws IOException { + List devList = PureJavaHidApi.enumerateDevices(); + HidDevice result = null; + for (purejavahidapi.HidDeviceInfo info : devList) { + String real_path = info.getPath(); + if (real_path.equals(path)) { + return new PureJavaHidDevice(PureJavaHidApi.openDevice(info)); + } + } + + return result; + } +} diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java index eb28953..071ffd9 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java @@ -38,17 +38,17 @@ import lombok.Synchronized; import lombok.extern.java.Log; import net.ash.HIDToVPADNetworkClient.controller.Controller; import net.ash.HIDToVPADNetworkClient.controller.Controller.ControllerType; +import net.ash.HIDToVPADNetworkClient.controller.HidController; import net.ash.HIDToVPADNetworkClient.controller.LinuxDevInputController; -import net.ash.HIDToVPADNetworkClient.controller.PureJavaHidController; import net.ash.HIDToVPADNetworkClient.controller.XInput13Controller; import net.ash.HIDToVPADNetworkClient.controller.XInput14Controller; import net.ash.HIDToVPADNetworkClient.controller.XInputController; import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException; +import net.ash.HIDToVPADNetworkClient.hid.HidDeviceInfo; +import net.ash.HIDToVPADNetworkClient.hid.HidManager; import net.ash.HIDToVPADNetworkClient.util.MessageBox; import net.ash.HIDToVPADNetworkClient.util.MessageBoxManager; -import net.ash.HIDToVPADNetworkClient.util.PureJavaHidApiManager; import net.ash.HIDToVPADNetworkClient.util.Settings; -import purejavahidapi.HidDeviceInfo; @Log public final class ControllerManager { @@ -104,9 +104,9 @@ public final class ControllerManager { if (!contains) { Controller c = null; switch (entry.getValue()) { - case PureJAVAHid: + case HIDController: try { - c = PureJavaHidController.getInstance(deviceIdentifier); + c = HidController.getInstance(deviceIdentifier); } catch (ControllerInitializationFailedException e) { // e.printStackTrace(); } catch (IOException e) { @@ -157,10 +157,9 @@ public final class ControllerManager { private static Map detectHIDDevices() { Map connectedDevices = new HashMap(); - System.out.println("detectHIDDevices"); - for (HidDeviceInfo info : PureJavaHidApiManager.getAttachedController()) { + for (HidDeviceInfo info : HidManager.getAttachedController()) { String path = info.getPath(); - connectedDevices.put(path, ControllerType.PureJAVAHid); + connectedDevices.put(path, ControllerType.HIDController); synchronized (connectedDevicesInfo) { connectedDevicesInfo.put(path, info); }