From e71d760cde9eab8942ac59d82780f752daa02ce2 Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 5 Apr 2017 11:44:21 +0200 Subject: [PATCH 1/4] 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); } From 367d2c79b6eca1d8d0d5c4ebd54cbf0efb29d7d5 Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 5 Apr 2017 18:14:58 +0200 Subject: [PATCH 2/4] removed the HidDeviceInfo class and added more methods to the HidDevice interface (still untested) --- .../controller/HidController.java | 8 ++- .../HIDToVPADNetworkClient/hid/HidDevice.java | 6 +++ .../hid/HidDeviceInfo.java | 33 ------------ .../hid/HidManager.java | 2 +- .../hid/HidManagerBackend.java | 16 +++--- .../hid/hid4java/Hid4JavaHidDevice.java | 26 +++++++-- .../hid/hid4java/Hid4JavaHidDeviceInfo.java | 54 ------------------- .../hid4java/Hid4JavaHidManagerBackend.java | 7 ++- .../hid/purejavahid/PureJavaHidDevice.java | 36 ++++++++++--- .../purejavahid/PureJavaHidDeviceInfo.java | 53 ------------------ .../PureJavaHidManagerBackend.java | 9 ++-- .../manager/ControllerManager.java | 14 +---- 12 files changed, 78 insertions(+), 186 deletions(-) delete mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/HidDeviceInfo.java delete mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDeviceInfo.java delete mode 100644 src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDeviceInfo.java 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); - } - } From 2a5dcddd69d0a1a06b9db4f6e20596872b98e64a Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 5 Apr 2017 19:40:16 +0200 Subject: [PATCH 3/4] Giving the Thread proper names. Using getUsage instead of getUsagePage --- src/net/ash/HIDToVPADNetworkClient/Main.java | 4 ++-- .../controller/Controller.java | 5 +++-- .../controller/HidController.java | 6 +++++- ...ontrollerInitializationFailedException.java | 4 ++++ .../HIDToVPADNetworkClient/hid/HidDevice.java | 2 +- .../hid/HidManagerBackend.java | 4 ++-- .../hid/hid4java/Hid4JavaHidDevice.java | 18 +++++++++++------- .../hid/purejavahid/PureJavaHidDevice.java | 2 +- .../manager/ActiveControllerManager.java | 4 ++-- .../manager/ControllerManager.java | 13 ++++++------- .../util/MessageBoxManager.java | 2 +- .../HIDToVPADNetworkClient/util/Settings.java | 2 ++ 12 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/net/ash/HIDToVPADNetworkClient/Main.java b/src/net/ash/HIDToVPADNetworkClient/Main.java index 5b5cf7c..45cad29 100644 --- a/src/net/ash/HIDToVPADNetworkClient/Main.java +++ b/src/net/ash/HIDToVPADNetworkClient/Main.java @@ -37,8 +37,8 @@ public final class Main { public static void main(String[] args) { Settings.loadSettings(); try { - new Thread(ActiveControllerManager.getInstance()).start(); - new Thread(NetworkManager.getInstance()).start(); + new Thread(ActiveControllerManager.getInstance(), "ActiveControllerManager").start(); + new Thread(NetworkManager.getInstance(), "NetworkManager").start(); } catch (Exception e) { e.printStackTrace(); fatal(); diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java index 0f49235..af5d1e5 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/Controller.java @@ -58,7 +58,7 @@ public abstract class Controller implements Runnable { this.type = type; this.identifier = identifier; if (!initController(identifier)) { - throw new ControllerInitializationFailedException(); + throw new ControllerInitializationFailedException("Initialization failed"); } } @@ -66,17 +66,18 @@ public abstract class Controller implements Runnable { public void run() { boolean shutdownState = shutdown; while (!shutdownState) { - Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL); 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); } + // System.out.println("data:" + Utilities.ByteArrayToString(newData)); setLatestData(newData); } doSleepAfterPollingData(); } + Utilities.sleep(Settings.DETECT_CONTROLLER_ACTIVE_INTERVAL); synchronized (shutdownLock) { shutdownState = shutdown; } diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java b/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java index 46ed24a..fb0f481 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java @@ -30,12 +30,14 @@ import lombok.extern.java.Log; import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException; import net.ash.HIDToVPADNetworkClient.hid.HidDevice; import net.ash.HIDToVPADNetworkClient.hid.HidManager; +import net.ash.HIDToVPADNetworkClient.util.Utilities; @Log public class HidController extends Controller { @Getter @Setter(AccessLevel.PRIVATE) private HidDevice hidDevice; public static Controller getInstance(String deviceIdentifier) throws IOException, ControllerInitializationFailedException { + HidDevice device = HidManager.getDeviceByPath(deviceIdentifier); short vid = 0; @@ -63,6 +65,7 @@ public class HidController extends Controller { public boolean initController(String identifier) { try { HidDevice device = HidManager.getDeviceByPath(identifier); + if (device == null || !device.open()) { return false; } @@ -78,7 +81,8 @@ public class HidController extends Controller { @Override public byte[] pollLatestData() { - return hidDevice.getLatestData(); + byte[] result = hidDevice.getLatestData(); + return result; } @Override diff --git a/src/net/ash/HIDToVPADNetworkClient/exeption/ControllerInitializationFailedException.java b/src/net/ash/HIDToVPADNetworkClient/exeption/ControllerInitializationFailedException.java index bc09711..15ebd7d 100644 --- a/src/net/ash/HIDToVPADNetworkClient/exeption/ControllerInitializationFailedException.java +++ b/src/net/ash/HIDToVPADNetworkClient/exeption/ControllerInitializationFailedException.java @@ -23,6 +23,10 @@ package net.ash.HIDToVPADNetworkClient.exeption; public class ControllerInitializationFailedException extends Exception { + public ControllerInitializationFailedException(String string) { + super(string); + } + /** * */ diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java b/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java index 6c341aa..15391de 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/HidDevice.java @@ -31,7 +31,7 @@ public interface HidDevice { byte[] getLatestData(); - short getUsagePage(); + short getUsage(); String getPath(); diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java b/src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java index a818f13..e1cef57 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/HidManagerBackend.java @@ -55,8 +55,8 @@ public abstract class HidManagerBackend { 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)); + short usage = info.getUsage(); + return (usage == 0x05 || usage == 0x04 || isNintendoController(info) || isPlaystationController(info)); } private static boolean isPlaystationController(HidDevice info) { diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java index ce0f361..9d9c661 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/hid4java/Hid4JavaHidDevice.java @@ -26,7 +26,7 @@ import java.util.Arrays; import net.ash.HIDToVPADNetworkClient.hid.HidDevice; class Hid4JavaHidDevice implements HidDevice { - private final org.hid4java.HidDevice myDevice; + private org.hid4java.HidDevice myDevice; private final byte[] data = new byte[64]; @@ -57,18 +57,22 @@ class Hid4JavaHidDevice implements HidDevice { @Override public byte[] getLatestData() { int length = myDevice.read(data); - if (length <= 0) return null; + if (length <= 0) return new byte[0]; return Arrays.copyOf(data, length); } - @Override - public short getUsagePage() { - return (short) myDevice.getUsagePage(); - } - @Override public String getPath() { return myDevice.getPath(); } + @Override + public String toString() { + return "Hid4JavaHidDevice [myDevice=" + myDevice + ", data=" + Arrays.toString(data) + "]"; + } + + @Override + public short getUsage() { + return (short) myDevice.getUsage(); + } } diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java index 7220e79..c7b28a6 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java @@ -81,7 +81,7 @@ class PureJavaHidDevice implements HidDevice, InputReportListener { } @Override - public short getUsagePage() { + public short getUsage() { return myDeviceInfo.getUsagePage(); } diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java index 85acf31..e57cd4a 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java @@ -61,7 +61,7 @@ public final class ActiveControllerManager implements Runnable { Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL); } } - }).start(); + }, "DetectControllerThread").start(); new Thread(new Runnable() { @Override @@ -71,7 +71,7 @@ public final class ActiveControllerManager implements Runnable { Utilities.sleep(Settings.HANDLE_INPUTS_INTERVAL); } } - }).start(); + }, "HandleControllerInputThread").start(); } public void updateControllerStates() { diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java index 742024a..a9f63a6 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java @@ -70,7 +70,7 @@ public final class ControllerManager { if (Settings.isLinux()) { connectedDevices.putAll(detectLinuxControllers()); } else if (Settings.isWindows()) { - connectedDevices.putAll(detectWindowsControllers()); + connectedDevices.putAll(detectXInputControllers()); } connectedDevices.putAll(detectHIDDevices()); @@ -79,7 +79,6 @@ public final class ControllerManager { List toRemove = new ArrayList(); synchronized (attachedControllers) { for (String s : attachedControllers.keySet()) { - System.out.println(s); if (!connectedDevices.containsKey(s)) { toRemove.add(s); } @@ -107,7 +106,7 @@ public final class ControllerManager { try { c = HidController.getInstance(deviceIdentifier); } catch (ControllerInitializationFailedException e) { - // e.printStackTrace(); + log.info(e.getMessage()); } catch (IOException e) { e.printStackTrace(); } @@ -116,14 +115,14 @@ public final class ControllerManager { try { c = new LinuxDevInputController(deviceIdentifier); } catch (ControllerInitializationFailedException e) { - // e.printStackTrace(); + log.info(e.getMessage()); } break; case XINPUT14: try { c = new XInput14Controller(deviceIdentifier); } catch (ControllerInitializationFailedException e) { - // e.printStackTrace(); + log.info(e.getMessage()); } break; case XINPUT13: @@ -140,7 +139,7 @@ public final class ControllerManager { if (Settings.AUTO_ACTIVATE_CONTROLLER) { c.setActive(true); } - new Thread(c).start(); + new Thread(c, "Controller Thread " + deviceIdentifier.substring(0, 50)).start(); synchronized (attachedControllers) { attachedControllers.put(deviceIdentifier, c); } @@ -164,7 +163,7 @@ public final class ControllerManager { return connectedDevices; } - private static Map detectWindowsControllers() { + private static Map detectXInputControllers() { Map result = new HashMap(); ControllerType type = ControllerType.XINPUT13; diff --git a/src/net/ash/HIDToVPADNetworkClient/util/MessageBoxManager.java b/src/net/ash/HIDToVPADNetworkClient/util/MessageBoxManager.java index b430582..8fa7026 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/MessageBoxManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/MessageBoxManager.java @@ -73,7 +73,7 @@ public final class MessageBoxManager implements Runnable { public static void addMessageBoxListener(MessageBoxListener msglistener) { if (!threadStarted) { - new Thread(instance).start(); + new Thread(instance, "MessageBoxManager").start(); threadStarted = true; } newList.add(msglistener); diff --git a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java index f1a0b58..8e741d7 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java @@ -44,6 +44,8 @@ public final class Settings { public static final int PING_INTERVAL = 1000; public static final int PROCESS_CMD_INTERVAL = 10; + public static final int DETECT_CONTROLLER_ACTIVE_INTERVAL = 100; + public static boolean SCAN_AUTOMATICALLY_FOR_CONTROLLERS = !isMacOSX(); // It doesn't work on OSX public static boolean DEBUG_UDP_OUTPUT = false; From 92baf6ebeb60b63a006b80fb1b8e2761b581bce7 Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 5 Apr 2017 11:07:02 -0700 Subject: [PATCH 4/4] Added Auto-Scanning for OSX and changed some debug messages --- .../controller/DS4NewController.java | 5 +++-- .../HIDToVPADNetworkClient/controller/HidController.java | 2 +- .../ash/HIDToVPADNetworkClient/gui/GuiInputControls.java | 6 +----- .../HIDToVPADNetworkClient/manager/ControllerManager.java | 4 +++- .../HIDToVPADNetworkClient/network/NetworkManager.java | 8 ++++---- src/net/ash/HIDToVPADNetworkClient/util/Settings.java | 2 +- 6 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/DS4NewController.java b/src/net/ash/HIDToVPADNetworkClient/controller/DS4NewController.java index b9e2a78..e4aa4c4 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/DS4NewController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/DS4NewController.java @@ -20,8 +20,9 @@ public class DS4NewController extends HidController { @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 + byte[] currentData = super.pollLatestData(); + if (Settings.isMacOSX() && currentData != null && currentData.length > 6) { // for some reason the controller has one extra byte at the beginning under + // OSX currentData = Arrays.copyOfRange(currentData, 1, 7); } return currentData; diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java b/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java index fb0f481..c753d18 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java @@ -30,7 +30,6 @@ import lombok.extern.java.Log; import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException; import net.ash.HIDToVPADNetworkClient.hid.HidDevice; import net.ash.HIDToVPADNetworkClient.hid.HidManager; -import net.ash.HIDToVPADNetworkClient.util.Utilities; @Log public class HidController extends Controller { @@ -69,6 +68,7 @@ public class HidController extends Controller { if (device == null || !device.open()) { return false; } + log.info("HidDevice opened!"); setHidDevice(device); return true; diff --git a/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java b/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java index e8b2067..dccbfdc 100644 --- a/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java +++ b/src/net/ash/HIDToVPADNetworkClient/gui/GuiInputControls.java @@ -61,11 +61,7 @@ public final class GuiInputControls extends JPanel { connectButton.setAlignmentX(Component.CENTER_ALIGNMENT); final JCheckBox cbautoScanForController = new JCheckBox(); - if (Settings.isMacOSX()) { - cbautoScanForController.setEnabled(false); - } else { - cbautoScanForController.setSelected(Settings.SCAN_AUTOMATICALLY_FOR_CONTROLLERS); - } + cbautoScanForController.setSelected(Settings.SCAN_AUTOMATICALLY_FOR_CONTROLLERS); cbautoScanForController.addActionListener(new ActionListener() { diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java index a9f63a6..246f963 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java @@ -89,6 +89,7 @@ public final class ControllerManager { synchronized (attachedControllers) { attachedControllers.get(remove).destroyAll(); attachedControllers.remove(remove); + log.info("Device removed: " + toRemove); } } @@ -139,10 +140,11 @@ public final class ControllerManager { if (Settings.AUTO_ACTIVATE_CONTROLLER) { c.setActive(true); } - new Thread(c, "Controller Thread " + deviceIdentifier.substring(0, 50)).start(); + new Thread(c, "Controller Thread " + deviceIdentifier).start(); synchronized (attachedControllers) { attachedControllers.put(deviceIdentifier, c); } + log.info("Device added: " + deviceIdentifier); } } } diff --git a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java index 595d674..a26c0c8 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java @@ -157,17 +157,17 @@ public final class NetworkManager implements Runnable { } } - // TODO: PONG from WiiU? Hey Quark ;) private void sendPing(PingCommand command) { if (sendTCP(Protocol.getRawPingDataToSend(command))) { - log.info("PING"); byte pong; try { pong = tcpClient.recvByte(); - if (pong != Protocol.TCP_CMD_PONG) { + if (pong == Protocol.TCP_CMD_PONG) { + log.info("Ping...Pong!"); + } else { + log.info("Got no valid response to a Ping. Disconnecting."); disconnect(); } - log.info("got PONG!"); } catch (IOException e) { log.info("Failed to get PONG. Disconnecting."); tcpClient.checkShouldRetry(); diff --git a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java index 8e741d7..fec12c0 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java @@ -46,7 +46,7 @@ public final class Settings { public static final int DETECT_CONTROLLER_ACTIVE_INTERVAL = 100; - public static boolean SCAN_AUTOMATICALLY_FOR_CONTROLLERS = !isMacOSX(); // It doesn't work on OSX + public static boolean SCAN_AUTOMATICALLY_FOR_CONTROLLERS = true; public static boolean DEBUG_UDP_OUTPUT = false; public static boolean SEND_DATA_ONLY_ON_CHANGE = false;