Minor code clean up

This commit is contained in:
Maschell 2017-04-13 11:23:02 +02:00
parent b94ed1cc9a
commit f5f1d42d1b
9 changed files with 42 additions and 41 deletions

View File

@ -113,13 +113,13 @@ public class HidController extends Controller {
// TODO: own class for joycons // TODO: own class for joycons
if (getVID() == 0x57e) { if (getVID() == 0x57e) {
if (getPID() == 0x2006) { if (getPID() == 0x2006) {
return "Joy-Con (L) (57e:2006) on " + getIdentifier(); return "Joy-Con (L) (0x057e:0x2006) on " + getIdentifier();
} else if (getPID() == 0x2007) { } else if (getPID() == 0x2007) {
return "Joy-Con (R) (57e:2007) on " + getIdentifier(); return "Joy-Con (R) (0x057e:0x2007) on " + getIdentifier();
} }
} }
String name = getHidDevice().getProductString(); 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());
} }
} }

View File

@ -41,10 +41,10 @@ public class LinuxDevInputController extends Controller implements Runnable {
private static final byte JS_EVENT_AXIS = 0x02; private static final byte JS_EVENT_AXIS = 0x02;
private DataInputStream controller; private DataInputStream controller;
@Getter private short VID; @Getter private short VID;
@Getter private short PID; @Getter private short PID;
private String name; private String name;
private long buttonState = 0; private long buttonState = 0;
@ -70,7 +70,7 @@ public class LinuxDevInputController extends Controller implements Runnable {
e.printStackTrace(); e.printStackTrace();
System.err.println("oops"); System.err.println("oops");
} }
if (VID == 0 || PID == 0) { if (VID == 0 || PID == 0) {
VID = ((short) (identifier.hashCode() & 0xFFFF)); VID = ((short) (identifier.hashCode() & 0xFFFF));
PID = ((short) ((identifier.hashCode() >> (Short.SIZE / Byte.SIZE)) & 0xFFFF)); PID = ((short) ((identifier.hashCode() >> (Short.SIZE / Byte.SIZE)) & 0xFFFF));
@ -81,29 +81,29 @@ public class LinuxDevInputController extends Controller implements Runnable {
return true; return true;
} }
//This could probably do with some cleanup // This could probably do with some cleanup
public void doSysFs(String identifier) throws Exception { public void doSysFs(String identifier) throws Exception {
Process querySysFs = Runtime.getRuntime().exec("udevadm info -q path " + identifier); Process querySysFs = Runtime.getRuntime().exec("udevadm info -q path " + identifier);
querySysFs.waitFor(); querySysFs.waitFor();
String sysfs_path = "/sys" + Utilities.getStringFromInputStream(querySysFs.getInputStream()).trim() + "/device"; String sysfs_path = "/sys" + Utilities.getStringFromInputStream(querySysFs.getInputStream()).trim() + "/device";
querySysFs.destroy(); querySysFs.destroy();
File sysfs = new File(sysfs_path); File sysfs = new File(sysfs_path);
if (!sysfs.exists()) return; if (!sysfs.exists()) return;
char[] nameBuf = new char[1024]; char[] nameBuf = new char[1024];
FileReader nameGet = new FileReader(sysfs_path + "/name"); FileReader nameGet = new FileReader(sysfs_path + "/name");
nameGet.read(nameBuf); nameGet.read(nameBuf);
nameGet.close(); nameGet.close();
name = new String(nameBuf).trim(); name = new String(nameBuf).trim();
char[] vidBuf = new char[6]; char[] vidBuf = new char[6];
FileReader vidGet = new FileReader(sysfs_path + "/id/vendor"); FileReader vidGet = new FileReader(sysfs_path + "/id/vendor");
vidGet.read(vidBuf); vidGet.read(vidBuf);
vidGet.close(); vidGet.close();
short vid = Short.parseShort(new String(vidBuf).trim(), 16); short vid = Short.parseShort(new String(vidBuf).trim(), 16);
this.VID = vid; this.VID = vid;
char[] pidBuf = new char[6]; char[] pidBuf = new char[6];
FileReader pidGet = new FileReader(sysfs_path + "/id/product"); FileReader pidGet = new FileReader(sysfs_path + "/id/product");
pidGet.read(pidBuf); pidGet.read(pidBuf);
@ -111,7 +111,7 @@ public class LinuxDevInputController extends Controller implements Runnable {
short pid = Short.parseShort(new String(pidBuf).trim(), 16); short pid = Short.parseShort(new String(pidBuf).trim(), 16);
this.PID = pid; this.PID = pid;
} }
@Override @Override
public byte[] pollLatestData() { public byte[] pollLatestData() {
DataInputStream inputStream = this.controller; DataInputStream inputStream = this.controller;
@ -125,7 +125,7 @@ public class LinuxDevInputController extends Controller implements Runnable {
type = inputStream.readByte(); type = inputStream.readByte();
number = inputStream.readByte(); number = inputStream.readByte();
} catch (IOException e) { } 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!"); System.err.println("[LinuxDevInputController] Couldn't read from controller!");
e.printStackTrace(); e.printStackTrace();
System.out.println("[LinuxDevInputController] Detaching..."); 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++) { for (int i = (Long.SIZE / Byte.SIZE); i < CONTROLLER_DATA_SIZE; i++) {
newData[i] = axisState[i - (Long.SIZE / Byte.SIZE)]; newData[i] = axisState[i - (Long.SIZE / Byte.SIZE)];
} }
return newData; return newData;
} }
@Override @Override
protected void doSleepAfterPollingData() { protected void doSleepAfterPollingData() {
// This is event driven (aka pollLatestData() is blocking anyway until // This is event driven (aka pollLatestData() is blocking anyway until we have data), we don't need to sleep it all.
// we have data), we don't need to sleep it all.
} }
@Override @Override
@ -195,6 +194,6 @@ public class LinuxDevInputController extends Controller implements Runnable {
@Override @Override
public String getInfoText() { 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());
} }
} }

View File

@ -30,6 +30,6 @@ public class XInput13Controller extends XInputController {
@Override @Override
public String getInfoText() { 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();
} }
} }

View File

@ -30,6 +30,6 @@ public class XInput14Controller extends XInputController {
@Override @Override
public String getInfoText() { 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();
} }
} }

View File

@ -139,6 +139,6 @@ public class XInputController extends Controller {
@Override @Override
public String getInfoText() { 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();
} }
} }

View File

@ -44,7 +44,7 @@ public class HidManager {
for (HidDevice info : backend.enumerateDevices()) { for (HidDevice info : backend.enumerateDevices()) {
if (isGamepad(info)) { if (isGamepad(info)) {
if (Settings.ControllerFiltering.getFilterState(Settings.ControllerFiltering.Type.HIDGAMEPAD)) { 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()) { if (isXboxController(info) && Settings.isWindows()) {
continue; continue;
} }
@ -72,7 +72,7 @@ public class HidManager {
public static boolean isGamepad(HidDevice info) { public static boolean isGamepad(HidDevice info) {
if (info == null) return false; if (info == null) return false;
short usage = info.getUsageID(); 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) { public static boolean isKeyboard(HidDevice info) {

View File

@ -113,7 +113,7 @@ class PureJavaHidDevice implements HidDevice, InputReportListener {
@Override @Override
public String getProductString() { public String getProductString() {
return myDeviceInfo.getProductString(); return myDeviceInfo.getProductString().trim();
} }
@Override @Override

View File

@ -209,11 +209,12 @@ public final class Settings {
public static class ControllerFiltering { public static class ControllerFiltering {
public static enum Type { public static enum Type {
HIDGAMEPAD (0, "HID Gamepads", Platform.LINUX.mask | Platform.WINDOWS.mask | Platform.MAC_OS_X.mask), 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), HIDKEYBOARD (1, "HID Keyboards", Platform.LINUX.mask | Platform.MAC_OS_X.mask),
HIDMOUSE (2, "HID Mice", Platform.LINUX.mask), HIDMOUSE (2, "HID Mice", Platform.LINUX.mask),
HIDOTHER (3, "Other HIDs", Platform.LINUX.mask | Platform.WINDOWS.mask | Platform.MAC_OS_X.mask), HIDOTHER (3, "Other HIDs", Platform.LINUX.mask | Platform.WINDOWS.mask | Platform.MAC_OS_X.mask),
LINUX (4, "Linux controllers", Platform.LINUX.mask), LINUX (4, "Linux controllers", Platform.LINUX.mask),
XINPUT (5, "XInput controllers", Platform.WINDOWS.mask); ;
private int index; private int index;
@Getter private String name; @Getter private String name;

View File

@ -8,32 +8,33 @@ import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
public class StatusReport { public class StatusReport {
public static String generateStatusReport() { public static String generateStatusReport() {
String report = "HID to VPAD Network Client\n\nRunning on "; StringBuilder report = new StringBuilder();
report += Settings.getPlattform(); report.append("HID to VPAD Network Client\n\nRunning on ");
report.append(Settings.getPlattform());
report += "\nHID Backend: "; report.append(System.lineSeparator()).append("HID Backend: ");
report += HidManager.getBackendType(); report.append(HidManager.getBackendType());
report += "\nCurrently "; report.append(System.lineSeparator()).append("Currently ");
report += (NetworkManager.getInstance().isConnected()) ? "Connected.\n" : "Disconnected.\n"; report.append((NetworkManager.getInstance().isConnected()) ? "Connected.\n" : "Disconnected.").append(System.lineSeparator());
report += (NetworkManager.getInstance().isReconnecting()) ? "" : "Not "; report.append((NetworkManager.getInstance().isReconnecting()) ? "" : "Not ");
report += "Reconnecting."; report.append("Reconnecting.");
report += "\n\nCurrently attached controllers:"; report.append(System.lineSeparator()).append(System.lineSeparator()).append("Currently attached controllers:");
for (Controller c : ControllerManager.getAttachedControllers()) { for (Controller c : ControllerManager.getAttachedControllers()) {
report += "\n"; report.append(System.lineSeparator());
report += c.toString(); report.append(c.toString());
} }
report += "\n\nFiltering settings:\n"; report.append(System.lineSeparator()).append(System.lineSeparator()).append("Filtering settings:").append(System.lineSeparator());
report += Settings.ControllerFiltering.getFilterStates(); report.append(Settings.ControllerFiltering.getFilterStates());
report += "\n\nAll HIDs:"; report.append(System.lineSeparator()).append(System.lineSeparator()).append("All HIDs:");
for (HidDevice d : HidManager.getAllAttachedControllers()) { for (HidDevice d : HidManager.getAllAttachedControllers()) {
report += "\n"; report.append(System.lineSeparator());
report += d.toString(); report.append(d.toString());
} }
return report; return report.toString();
} }
} }