diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java b/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java index 662c7d1..46ed24a 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java @@ -43,7 +43,6 @@ public class HidController extends Controller { if (device != null) { vid = device.getVendorId(); pid = device.getProductId(); - device.close(); } // We use a special version to optimize the data for the switch pro controller @@ -62,10 +61,9 @@ public class HidController extends Controller { @Override public boolean initController(String identifier) { - HidDevice device; try { - device = HidManager.getDeviceByPath(identifier); - if (device == null) { + HidDevice device = HidManager.getDeviceByPath(identifier); + if (device == null || !device.open()) { return false; } @@ -108,7 +106,7 @@ public class HidController extends Controller { @Override public String getInfoText() { - // TODO: + // TODO: own class for joycons if (getVID() == 0x57e) { if (getPID() == 0x2006) { return "Joy-Con (L) on " + getIdentifier(); diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java b/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java index 47a6b06..6c341aa 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java @@ -31,4 +31,10 @@ public interface HidDevice { byte[] getLatestData(); + short getUsagePage(); + + String getPath(); + + boolean open(); + } diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/HidDeviceInfo.java b/src/net/ash/HIDToVPADNetworkClient/hid/HidDeviceInfo.java deleted file mode 100644 index ce7bcd4..0000000 --- a/src/net/ash/HIDToVPADNetworkClient/hid/HidDeviceInfo.java +++ /dev/null @@ -1,33 +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.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 index 4b0330e..92adccc 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/HidManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/HidManager.java @@ -31,7 +31,7 @@ import net.ash.HIDToVPADNetworkClient.util.Settings; public class HidManager { private final static HidManagerBackend backend; - public static List getAttachedController() { + public static List getAttachedController() { return backend.getAttachedController(); } diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java b/src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java index 2ee7e35..a818f13 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java @@ -38,10 +38,10 @@ public abstract class HidManagerBackend { */ public abstract HidDevice getDeviceByPath(String path) throws IOException; - public List getAttachedController() { - List connectedGamepads = new ArrayList(); + public List getAttachedController() { + List connectedGamepads = new ArrayList(); - for (HidDeviceInfo info : enumerateDevices()) { + for (HidDevice info : enumerateDevices()) { if (isGamepad(info)) { // Skip Xbox controller under windows. We should use XInput instead. if (isXboxController(info) && Settings.isWindows()) { @@ -53,26 +53,26 @@ public abstract class HidManagerBackend { return connectedGamepads; } - public static boolean isGamepad(HidDeviceInfo info) { + public static boolean isGamepad(HidDevice info) { if (info == null) return false; short usagePage = info.getUsagePage(); return (usagePage == 0x05 || usagePage == 0x01 || usagePage == 0x04 || isNintendoController(info) || isPlaystationController(info)); } - private static boolean isPlaystationController(HidDeviceInfo info) { + private static boolean isPlaystationController(HidDevice info) { if (info == null) return false; return (info.getVendorId() == 0x054c); } - private static boolean isNintendoController(HidDeviceInfo info) { + private static boolean isNintendoController(HidDevice info) { if (info == null) return false; return (info.getVendorId() == 0x57e); } - private static boolean isXboxController(HidDeviceInfo info) { + private static boolean isXboxController(HidDevice info) { if (info == null) return false; return (info.getVendorId() == 0x045e) && ((info.getProductId() == 0x02ff) || (info.getProductId() == 0x02a1)); } - public abstract List enumerateDevices(); + public abstract List enumerateDevices(); } diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java index 197bbc0..ce0f361 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java @@ -34,6 +34,16 @@ class Hid4JavaHidDevice implements HidDevice { this.myDevice = device; } + @Override + public boolean open() { + return myDevice.open(); + } + + @Override + public void close() { + myDevice.close(); + } + @Override public short getVendorId() { return myDevice.getVendorId(); @@ -44,15 +54,21 @@ class Hid4JavaHidDevice implements HidDevice { 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); } + + @Override + public short getUsagePage() { + return (short) myDevice.getUsagePage(); + } + + @Override + public String getPath() { + return myDevice.getPath(); + } + } diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDeviceInfo.java b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDeviceInfo.java deleted file mode 100644 index 8ca5d24..0000000 --- a/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDeviceInfo.java +++ /dev/null @@ -1,54 +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.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 index beabb21..d1dd1fe 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidManagerBackend.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidManagerBackend.java @@ -29,7 +29,6 @@ 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 { @@ -50,10 +49,10 @@ public class Hid4JavaHidManagerBackend extends HidManagerBackend { } @Override - public List enumerateDevices() { - List result = new ArrayList(); + public List enumerateDevices() { + List result = new ArrayList(); for (org.hid4java.HidDevice info : HidManager.getHidServices().getAttachedHidDevices()) { - result.add(new Hid4JavaHidDeviceInfo(info)); + result.add(new Hid4JavaHidDevice(info)); } return result; } diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java index f1f614e..7220e79 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java @@ -21,21 +21,23 @@ *******************************************************************************/ package net.ash.HIDToVPADNetworkClient.hid.purejavahid; +import java.io.IOException; import java.util.Arrays; import lombok.Synchronized; import net.ash.HIDToVPADNetworkClient.hid.HidDevice; +import purejavahidapi.HidDeviceInfo; import purejavahidapi.InputReportListener; class PureJavaHidDevice implements HidDevice, InputReportListener { - private final purejavahidapi.HidDevice myDevice; + private purejavahidapi.HidDevice myDevice = null; + private final purejavahidapi.HidDeviceInfo myDeviceInfo; private final Object dataLock = new Object(); protected byte[] currentData = new byte[1]; - public PureJavaHidDevice(purejavahidapi.HidDevice device) { - this.myDevice = device; - device.setInputReportListener(this); + public PureJavaHidDevice(HidDeviceInfo info) { + this.myDeviceInfo = info; } @Override @@ -46,12 +48,25 @@ class PureJavaHidDevice implements HidDevice, InputReportListener { @Override public short getVendorId() { - return myDevice.getHidDeviceInfo().getVendorId(); + return myDeviceInfo.getVendorId(); } @Override public short getProductId() { - return myDevice.getHidDeviceInfo().getProductId(); + return myDeviceInfo.getProductId(); + } + + @Override + public boolean open() { + boolean result = true; + try { + myDevice = purejavahidapi.PureJavaHidApi.openDevice(myDeviceInfo); + myDevice.setInputReportListener(this); + } catch (IOException e) { + result = false; + e.printStackTrace(); + } + return result; } @Override @@ -65,4 +80,13 @@ class PureJavaHidDevice implements HidDevice, InputReportListener { return currentData.clone(); } + @Override + public short getUsagePage() { + return myDeviceInfo.getUsagePage(); + } + + @Override + public String getPath() { + return myDeviceInfo.getPath(); + } } diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDeviceInfo.java b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDeviceInfo.java deleted file mode 100644 index f65c221..0000000 --- a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDeviceInfo.java +++ /dev/null @@ -1,53 +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.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 index 3d7c90a..32e4989 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidManagerBackend.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidManagerBackend.java @@ -26,17 +26,16 @@ 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(); + public List enumerateDevices() { + List result = new ArrayList(); for (purejavahidapi.HidDeviceInfo info : PureJavaHidApi.enumerateDevices()) { - result.add(new PureJavaHidDeviceInfo(info)); + result.add(new PureJavaHidDevice(info)); } return result; } @@ -48,7 +47,7 @@ public class PureJavaHidManagerBackend extends HidManagerBackend { for (purejavahidapi.HidDeviceInfo info : devList) { String real_path = info.getPath(); if (real_path.equals(path)) { - return new PureJavaHidDevice(PureJavaHidApi.openDevice(info)); + return new PureJavaHidDevice(info); } } diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java index 071ffd9..742024a 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java @@ -44,7 +44,7 @@ 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.HidDevice; import net.ash.HIDToVPADNetworkClient.hid.HidManager; import net.ash.HIDToVPADNetworkClient.util.MessageBox; import net.ash.HIDToVPADNetworkClient.util.MessageBoxManager; @@ -53,7 +53,6 @@ import net.ash.HIDToVPADNetworkClient.util.Settings; @Log public final class ControllerManager { private static final Map attachedControllers = new HashMap(); - private static final Map connectedDevicesInfo = new HashMap(); private static boolean threwUnsatisfiedLinkError = false; @@ -157,12 +156,9 @@ public final class ControllerManager { private static Map detectHIDDevices() { Map connectedDevices = new HashMap(); - for (HidDeviceInfo info : HidManager.getAttachedController()) { + for (HidDevice info : HidManager.getAttachedController()) { String path = info.getPath(); connectedDevices.put(path, ControllerType.HIDController); - synchronized (connectedDevicesInfo) { - connectedDevicesInfo.put(path, info); - } } return connectedDevices; @@ -244,10 +240,4 @@ public final class ControllerManager { c.setActive(false); } } - - @Synchronized("connectedDevicesInfo") - public static HidDeviceInfo getDeviceInfoByPath(String path) { - return connectedDevicesInfo.get(path); - } - }