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;