From f5f1d42d1be6e5bf0925f7fba33c26247bc7f7e1 Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 13 Apr 2017 11:23:02 +0200 Subject: [PATCH] Minor code clean up --- .../controller/HidController.java | 6 ++-- .../controller/LinuxDevInputController.java | 27 +++++++------- .../controller/XInput13Controller.java | 2 +- .../controller/XInput14Controller.java | 2 +- .../controller/XInputController.java | 2 +- .../hid/HidManager.java | 4 +-- .../hid/purejavahid/PureJavaHidDevice.java | 2 +- .../HIDToVPADNetworkClient/util/Settings.java | 3 +- .../util/StatusReport.java | 35 ++++++++++--------- 9 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java b/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java index 6ff2724..6bc5167 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/HidController.java @@ -113,13 +113,13 @@ public class HidController extends Controller { // TODO: own class for joycons if (getVID() == 0x57e) { if (getPID() == 0x2006) { - return "Joy-Con (L) (57e:2006) on " + getIdentifier(); + return "Joy-Con (L) (0x057e:0x2006) on " + getIdentifier(); } else if (getPID() == 0x2007) { - return "Joy-Con (R) (57e:2007) on " + getIdentifier(); + return "Joy-Con (R) (0x057e:0x2007) on " + getIdentifier(); } } String name = getHidDevice().getProductString(); - return ((name != null) ? name : "USB HID") + " (" + Integer.toHexString((int)getVID() & 0xFFFF) + ":" + Integer.toHexString((int)getPID() & 0xFFFF) + ") on " + getIdentifier(); + return String.format("%s (0x%04X:0x%04X) on %s",(name != null) ? name: "USB HID", getVID(),getPID(),getIdentifier()); } } \ No newline at end of file diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/LinuxDevInputController.java b/src/net/ash/HIDToVPADNetworkClient/controller/LinuxDevInputController.java index 3fa31f8..9b2c17b 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/LinuxDevInputController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/LinuxDevInputController.java @@ -41,10 +41,10 @@ public class LinuxDevInputController extends Controller implements Runnable { private static final byte JS_EVENT_AXIS = 0x02; private DataInputStream controller; - + @Getter private short VID; @Getter private short PID; - + private String name; private long buttonState = 0; @@ -70,7 +70,7 @@ public class LinuxDevInputController extends Controller implements Runnable { e.printStackTrace(); System.err.println("oops"); } - + if (VID == 0 || PID == 0) { VID = ((short) (identifier.hashCode() & 0xFFFF)); PID = ((short) ((identifier.hashCode() >> (Short.SIZE / Byte.SIZE)) & 0xFFFF)); @@ -81,29 +81,29 @@ public class LinuxDevInputController extends Controller implements Runnable { return true; } - //This could probably do with some cleanup + // This could probably do with some cleanup public void doSysFs(String identifier) throws Exception { Process querySysFs = Runtime.getRuntime().exec("udevadm info -q path " + identifier); querySysFs.waitFor(); - + String sysfs_path = "/sys" + Utilities.getStringFromInputStream(querySysFs.getInputStream()).trim() + "/device"; querySysFs.destroy(); File sysfs = new File(sysfs_path); if (!sysfs.exists()) return; - + char[] nameBuf = new char[1024]; FileReader nameGet = new FileReader(sysfs_path + "/name"); nameGet.read(nameBuf); nameGet.close(); name = new String(nameBuf).trim(); - + char[] vidBuf = new char[6]; FileReader vidGet = new FileReader(sysfs_path + "/id/vendor"); vidGet.read(vidBuf); vidGet.close(); short vid = Short.parseShort(new String(vidBuf).trim(), 16); this.VID = vid; - + char[] pidBuf = new char[6]; FileReader pidGet = new FileReader(sysfs_path + "/id/product"); pidGet.read(pidBuf); @@ -111,7 +111,7 @@ public class LinuxDevInputController extends Controller implements Runnable { short pid = Short.parseShort(new String(pidBuf).trim(), 16); this.PID = pid; } - + @Override public byte[] pollLatestData() { DataInputStream inputStream = this.controller; @@ -125,7 +125,7 @@ public class LinuxDevInputController extends Controller implements Runnable { type = inputStream.readByte(); number = inputStream.readByte(); } catch (IOException e) { - if (!isActive()) return null; //"Stream closed" when removing + if (!isActive()) return null; // "Stream closed" when removing System.err.println("[LinuxDevInputController] Couldn't read from controller!"); e.printStackTrace(); System.out.println("[LinuxDevInputController] Detaching..."); @@ -169,14 +169,13 @@ public class LinuxDevInputController extends Controller implements Runnable { for (int i = (Long.SIZE / Byte.SIZE); i < CONTROLLER_DATA_SIZE; i++) { newData[i] = axisState[i - (Long.SIZE / Byte.SIZE)]; } - + return newData; } @Override protected void doSleepAfterPollingData() { - // This is event driven (aka pollLatestData() is blocking anyway until - // we have data), we don't need to sleep it all. + // This is event driven (aka pollLatestData() is blocking anyway until we have data), we don't need to sleep it all. } @Override @@ -195,6 +194,6 @@ public class LinuxDevInputController extends Controller implements Runnable { @Override public String getInfoText() { - return ((name != null) ? name : "Linux controller") + " (" + Integer.toHexString((int)getVID() & 0xFFFF) + ":" + Integer.toHexString((int)getPID() & 0xFFFF) + ") on " + getIdentifier(); + return String.format("%s (0x%04X:0x%04X) on %s", (name != null) ? name : "Linux Controller", getVID(), getPID(), getIdentifier()); } } diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/XInput13Controller.java b/src/net/ash/HIDToVPADNetworkClient/controller/XInput13Controller.java index ca6ec70..1b8026c 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/XInput13Controller.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/XInput13Controller.java @@ -30,6 +30,6 @@ public class XInput13Controller extends XInputController { @Override public String getInfoText() { - return "XInput 1.3 (" + Integer.toHexString((int)getVID() & 0xFFFF) + ":" + Integer.toHexString((int)getPID() & 0xFFFF) + ") on " + getIdentifier(); + return String.format("XInput 1.3 (0x%04X:0x%04X) on ", getVID(),getPID()) + getIdentifier(); } } diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/XInput14Controller.java b/src/net/ash/HIDToVPADNetworkClient/controller/XInput14Controller.java index e2674f5..cf3dd45 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/XInput14Controller.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/XInput14Controller.java @@ -30,6 +30,6 @@ public class XInput14Controller extends XInputController { @Override public String getInfoText() { - return "XInput 1.4 (" + Integer.toHexString((int)getVID() & 0xFFFF) + ":" + Integer.toHexString((int)getPID() & 0xFFFF) + ") on " + getIdentifier(); + return String.format("XInput 1.4 (0x%04X:0x%04X) on ", getVID(),getPID()) + getIdentifier(); } } diff --git a/src/net/ash/HIDToVPADNetworkClient/controller/XInputController.java b/src/net/ash/HIDToVPADNetworkClient/controller/XInputController.java index efba649..3863888 100644 --- a/src/net/ash/HIDToVPADNetworkClient/controller/XInputController.java +++ b/src/net/ash/HIDToVPADNetworkClient/controller/XInputController.java @@ -139,6 +139,6 @@ public class XInputController extends Controller { @Override public String getInfoText() { - return "XInput (" + Integer.toHexString((int)getVID() & 0xFFFF) + ":" + Integer.toHexString((int)getPID() & 0xFFFF) + ") on " + getIdentifier(); + return String.format("XInput (0x%04X:%0x04X) on ", getVID(),getPID()) + getIdentifier(); } } diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/HidManager.java b/src/net/ash/HIDToVPADNetworkClient/hid/HidManager.java index f4bf2bb..35795b9 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/HidManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/HidManager.java @@ -44,7 +44,7 @@ public class HidManager { for (HidDevice info : backend.enumerateDevices()) { if (isGamepad(info)) { if (Settings.ControllerFiltering.getFilterState(Settings.ControllerFiltering.Type.HIDGAMEPAD)) { - // Skip Xbox controller under windows. We should use XInput instead. + // Skip Xbox controller under windows. We should use XInput instead. if (isXboxController(info) && Settings.isWindows()) { continue; } @@ -72,7 +72,7 @@ public class HidManager { public static boolean isGamepad(HidDevice info) { if (info == null) return false; short usage = info.getUsageID(); - return (usage == 0x05 || usage == 0x04 || isNintendoController(info) || isPlaystationController(info)); + return (info.getProductString().toLowerCase().contains("gamepad") || usage == 0x05 || usage == 0x04 || isNintendoController(info) || isPlaystationController(info)); } public static boolean isKeyboard(HidDevice info) { diff --git a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java index e33cf3f..3f4669c 100644 --- a/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java +++ b/src/net/ash/HIDToVPADNetworkClient/hid/purejavahid/PureJavaHidDevice.java @@ -113,7 +113,7 @@ class PureJavaHidDevice implements HidDevice, InputReportListener { @Override public String getProductString() { - return myDeviceInfo.getProductString(); + return myDeviceInfo.getProductString().trim(); } @Override diff --git a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java index b67617d..bf98d64 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/Settings.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/Settings.java @@ -209,11 +209,12 @@ public final class Settings { public static class ControllerFiltering { public static enum Type { HIDGAMEPAD (0, "HID Gamepads", Platform.LINUX.mask | Platform.WINDOWS.mask | Platform.MAC_OS_X.mask), + XINPUT (5, "XInput controllers", Platform.WINDOWS.mask), HIDKEYBOARD (1, "HID Keyboards", Platform.LINUX.mask | Platform.MAC_OS_X.mask), HIDMOUSE (2, "HID Mice", Platform.LINUX.mask), HIDOTHER (3, "Other HIDs", Platform.LINUX.mask | Platform.WINDOWS.mask | Platform.MAC_OS_X.mask), LINUX (4, "Linux controllers", Platform.LINUX.mask), - XINPUT (5, "XInput controllers", Platform.WINDOWS.mask); + ; private int index; @Getter private String name; diff --git a/src/net/ash/HIDToVPADNetworkClient/util/StatusReport.java b/src/net/ash/HIDToVPADNetworkClient/util/StatusReport.java index b6ab09f..fa6a735 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/StatusReport.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/StatusReport.java @@ -8,32 +8,33 @@ import net.ash.HIDToVPADNetworkClient.network.NetworkManager; public class StatusReport { public static String generateStatusReport() { - String report = "HID to VPAD Network Client\n\nRunning on "; - report += Settings.getPlattform(); + StringBuilder report = new StringBuilder(); + report.append("HID to VPAD Network Client\n\nRunning on "); + report.append(Settings.getPlattform()); - report += "\nHID Backend: "; - report += HidManager.getBackendType(); + report.append(System.lineSeparator()).append("HID Backend: "); + report.append(HidManager.getBackendType()); - report += "\nCurrently "; - report += (NetworkManager.getInstance().isConnected()) ? "Connected.\n" : "Disconnected.\n"; - report += (NetworkManager.getInstance().isReconnecting()) ? "" : "Not "; - report += "Reconnecting."; + report.append(System.lineSeparator()).append("Currently "); + report.append((NetworkManager.getInstance().isConnected()) ? "Connected.\n" : "Disconnected.").append(System.lineSeparator()); + report.append((NetworkManager.getInstance().isReconnecting()) ? "" : "Not "); + report.append("Reconnecting."); - report += "\n\nCurrently attached controllers:"; + report.append(System.lineSeparator()).append(System.lineSeparator()).append("Currently attached controllers:"); for (Controller c : ControllerManager.getAttachedControllers()) { - report += "\n"; - report += c.toString(); + report.append(System.lineSeparator()); + report.append(c.toString()); } - report += "\n\nFiltering settings:\n"; - report += Settings.ControllerFiltering.getFilterStates(); + report.append(System.lineSeparator()).append(System.lineSeparator()).append("Filtering settings:").append(System.lineSeparator()); + report.append(Settings.ControllerFiltering.getFilterStates()); - report += "\n\nAll HIDs:"; + report.append(System.lineSeparator()).append(System.lineSeparator()).append("All HIDs:"); for (HidDevice d : HidManager.getAllAttachedControllers()) { - report += "\n"; - report += d.toString(); + report.append(System.lineSeparator()); + report.append(d.toString()); } - return report; + return report.toString(); } }