Giving the Thread proper names. Using getUsage instead of getUsagePage

This commit is contained in:
Maschell 2017-04-05 19:40:16 +02:00
parent 367d2c79b6
commit 2a5dcddd69
12 changed files with 40 additions and 26 deletions

View File

@ -37,8 +37,8 @@ public final class Main {
public static void main(String[] args) { public static void main(String[] args) {
Settings.loadSettings(); Settings.loadSettings();
try { try {
new Thread(ActiveControllerManager.getInstance()).start(); new Thread(ActiveControllerManager.getInstance(), "ActiveControllerManager").start();
new Thread(NetworkManager.getInstance()).start(); new Thread(NetworkManager.getInstance(), "NetworkManager").start();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fatal(); fatal();

View File

@ -58,7 +58,7 @@ public abstract class Controller implements Runnable {
this.type = type; this.type = type;
this.identifier = identifier; this.identifier = identifier;
if (!initController(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() { public void run() {
boolean shutdownState = shutdown; boolean shutdownState = shutdown;
while (!shutdownState) { while (!shutdownState) {
Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL);
while (isActive()) { while (isActive()) {
byte[] newData = pollLatestData(); byte[] newData = pollLatestData();
if (newData != null && newData.length != 0) { if (newData != null && newData.length != 0) {
if (newData.length > MAX_PACKET_LENGTH) { if (newData.length > MAX_PACKET_LENGTH) {
newData = Arrays.copyOfRange(newData, 0, MAX_PACKET_LENGTH); newData = Arrays.copyOfRange(newData, 0, MAX_PACKET_LENGTH);
} }
// System.out.println("data:" + Utilities.ByteArrayToString(newData));
setLatestData(newData); setLatestData(newData);
} }
doSleepAfterPollingData(); doSleepAfterPollingData();
} }
Utilities.sleep(Settings.DETECT_CONTROLLER_ACTIVE_INTERVAL);
synchronized (shutdownLock) { synchronized (shutdownLock) {
shutdownState = shutdown; shutdownState = shutdown;
} }

View File

@ -30,12 +30,14 @@ import lombok.extern.java.Log;
import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException; import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException;
import net.ash.HIDToVPADNetworkClient.hid.HidDevice; import net.ash.HIDToVPADNetworkClient.hid.HidDevice;
import net.ash.HIDToVPADNetworkClient.hid.HidManager; import net.ash.HIDToVPADNetworkClient.hid.HidManager;
import net.ash.HIDToVPADNetworkClient.util.Utilities;
@Log @Log
public class HidController extends Controller { public class HidController extends Controller {
@Getter @Setter(AccessLevel.PRIVATE) private HidDevice hidDevice; @Getter @Setter(AccessLevel.PRIVATE) private HidDevice hidDevice;
public static Controller getInstance(String deviceIdentifier) throws IOException, ControllerInitializationFailedException { public static Controller getInstance(String deviceIdentifier) throws IOException, ControllerInitializationFailedException {
HidDevice device = HidManager.getDeviceByPath(deviceIdentifier); HidDevice device = HidManager.getDeviceByPath(deviceIdentifier);
short vid = 0; short vid = 0;
@ -63,6 +65,7 @@ public class HidController extends Controller {
public boolean initController(String identifier) { public boolean initController(String identifier) {
try { try {
HidDevice device = HidManager.getDeviceByPath(identifier); HidDevice device = HidManager.getDeviceByPath(identifier);
if (device == null || !device.open()) { if (device == null || !device.open()) {
return false; return false;
} }
@ -78,7 +81,8 @@ public class HidController extends Controller {
@Override @Override
public byte[] pollLatestData() { public byte[] pollLatestData() {
return hidDevice.getLatestData(); byte[] result = hidDevice.getLatestData();
return result;
} }
@Override @Override

View File

@ -23,6 +23,10 @@ package net.ash.HIDToVPADNetworkClient.exeption;
public class ControllerInitializationFailedException extends Exception { public class ControllerInitializationFailedException extends Exception {
public ControllerInitializationFailedException(String string) {
super(string);
}
/** /**
* *
*/ */

View File

@ -31,7 +31,7 @@ public interface HidDevice {
byte[] getLatestData(); byte[] getLatestData();
short getUsagePage(); short getUsage();
String getPath(); String getPath();

View File

@ -55,8 +55,8 @@ public abstract class HidManagerBackend {
public static boolean isGamepad(HidDevice info) { public static boolean isGamepad(HidDevice info) {
if (info == null) return false; if (info == null) return false;
short usagePage = info.getUsagePage(); short usage = info.getUsage();
return (usagePage == 0x05 || usagePage == 0x01 || usagePage == 0x04 || isNintendoController(info) || isPlaystationController(info)); return (usage == 0x05 || usage == 0x04 || isNintendoController(info) || isPlaystationController(info));
} }
private static boolean isPlaystationController(HidDevice info) { private static boolean isPlaystationController(HidDevice info) {

View File

@ -26,7 +26,7 @@ import java.util.Arrays;
import net.ash.HIDToVPADNetworkClient.hid.HidDevice; import net.ash.HIDToVPADNetworkClient.hid.HidDevice;
class Hid4JavaHidDevice implements HidDevice { class Hid4JavaHidDevice implements HidDevice {
private final org.hid4java.HidDevice myDevice; private org.hid4java.HidDevice myDevice;
private final byte[] data = new byte[64]; private final byte[] data = new byte[64];
@ -57,18 +57,22 @@ class Hid4JavaHidDevice implements HidDevice {
@Override @Override
public byte[] getLatestData() { public byte[] getLatestData() {
int length = myDevice.read(data); int length = myDevice.read(data);
if (length <= 0) return null; if (length <= 0) return new byte[0];
return Arrays.copyOf(data, length); return Arrays.copyOf(data, length);
} }
@Override
public short getUsagePage() {
return (short) myDevice.getUsagePage();
}
@Override @Override
public String getPath() { public String getPath() {
return myDevice.getPath(); return myDevice.getPath();
} }
@Override
public String toString() {
return "Hid4JavaHidDevice [myDevice=" + myDevice + ", data=" + Arrays.toString(data) + "]";
}
@Override
public short getUsage() {
return (short) myDevice.getUsage();
}
} }

View File

@ -81,7 +81,7 @@ class PureJavaHidDevice implements HidDevice, InputReportListener {
} }
@Override @Override
public short getUsagePage() { public short getUsage() {
return myDeviceInfo.getUsagePage(); return myDeviceInfo.getUsagePage();
} }

View File

@ -61,7 +61,7 @@ public final class ActiveControllerManager implements Runnable {
Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL); Utilities.sleep(Settings.DETECT_CONTROLLER_INTERVAL);
} }
} }
}).start(); }, "DetectControllerThread").start();
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
@ -71,7 +71,7 @@ public final class ActiveControllerManager implements Runnable {
Utilities.sleep(Settings.HANDLE_INPUTS_INTERVAL); Utilities.sleep(Settings.HANDLE_INPUTS_INTERVAL);
} }
} }
}).start(); }, "HandleControllerInputThread").start();
} }
public void updateControllerStates() { public void updateControllerStates() {

View File

@ -70,7 +70,7 @@ public final class ControllerManager {
if (Settings.isLinux()) { if (Settings.isLinux()) {
connectedDevices.putAll(detectLinuxControllers()); connectedDevices.putAll(detectLinuxControllers());
} else if (Settings.isWindows()) { } else if (Settings.isWindows()) {
connectedDevices.putAll(detectWindowsControllers()); connectedDevices.putAll(detectXInputControllers());
} }
connectedDevices.putAll(detectHIDDevices()); connectedDevices.putAll(detectHIDDevices());
@ -79,7 +79,6 @@ public final class ControllerManager {
List<String> toRemove = new ArrayList<String>(); List<String> toRemove = new ArrayList<String>();
synchronized (attachedControllers) { synchronized (attachedControllers) {
for (String s : attachedControllers.keySet()) { for (String s : attachedControllers.keySet()) {
System.out.println(s);
if (!connectedDevices.containsKey(s)) { if (!connectedDevices.containsKey(s)) {
toRemove.add(s); toRemove.add(s);
} }
@ -107,7 +106,7 @@ public final class ControllerManager {
try { try {
c = HidController.getInstance(deviceIdentifier); c = HidController.getInstance(deviceIdentifier);
} catch (ControllerInitializationFailedException e) { } catch (ControllerInitializationFailedException e) {
// e.printStackTrace(); log.info(e.getMessage());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -116,14 +115,14 @@ public final class ControllerManager {
try { try {
c = new LinuxDevInputController(deviceIdentifier); c = new LinuxDevInputController(deviceIdentifier);
} catch (ControllerInitializationFailedException e) { } catch (ControllerInitializationFailedException e) {
// e.printStackTrace(); log.info(e.getMessage());
} }
break; break;
case XINPUT14: case XINPUT14:
try { try {
c = new XInput14Controller(deviceIdentifier); c = new XInput14Controller(deviceIdentifier);
} catch (ControllerInitializationFailedException e) { } catch (ControllerInitializationFailedException e) {
// e.printStackTrace(); log.info(e.getMessage());
} }
break; break;
case XINPUT13: case XINPUT13:
@ -140,7 +139,7 @@ public final class ControllerManager {
if (Settings.AUTO_ACTIVATE_CONTROLLER) { if (Settings.AUTO_ACTIVATE_CONTROLLER) {
c.setActive(true); c.setActive(true);
} }
new Thread(c).start(); new Thread(c, "Controller Thread " + deviceIdentifier.substring(0, 50)).start();
synchronized (attachedControllers) { synchronized (attachedControllers) {
attachedControllers.put(deviceIdentifier, c); attachedControllers.put(deviceIdentifier, c);
} }
@ -164,7 +163,7 @@ public final class ControllerManager {
return connectedDevices; return connectedDevices;
} }
private static Map<String, ControllerType> detectWindowsControllers() { private static Map<String, ControllerType> detectXInputControllers() {
Map<String, ControllerType> result = new HashMap<String, ControllerType>(); Map<String, ControllerType> result = new HashMap<String, ControllerType>();
ControllerType type = ControllerType.XINPUT13; ControllerType type = ControllerType.XINPUT13;

View File

@ -73,7 +73,7 @@ public final class MessageBoxManager implements Runnable {
public static void addMessageBoxListener(MessageBoxListener msglistener) { public static void addMessageBoxListener(MessageBoxListener msglistener) {
if (!threadStarted) { if (!threadStarted) {
new Thread(instance).start(); new Thread(instance, "MessageBoxManager").start();
threadStarted = true; threadStarted = true;
} }
newList.add(msglistener); newList.add(msglistener);

View File

@ -44,6 +44,8 @@ public final class Settings {
public static final int PING_INTERVAL = 1000; public static final int PING_INTERVAL = 1000;
public static final int PROCESS_CMD_INTERVAL = 10; 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 SCAN_AUTOMATICALLY_FOR_CONTROLLERS = !isMacOSX(); // It doesn't work on OSX
public static boolean DEBUG_UDP_OUTPUT = false; public static boolean DEBUG_UDP_OUTPUT = false;