mirror of
https://github.com/Maschell/HIDtoVPADNetworkClient.git
synced 2024-11-21 21:49:17 +01:00
Giving the Thread proper names. Using getUsage instead of getUsagePage
This commit is contained in:
parent
367d2c79b6
commit
2a5dcddd69
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -23,6 +23,10 @@ package net.ash.HIDToVPADNetworkClient.exeption;
|
||||
|
||||
public class ControllerInitializationFailedException extends Exception {
|
||||
|
||||
public ControllerInitializationFailedException(String string) {
|
||||
super(string);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
@ -31,7 +31,7 @@ public interface HidDevice {
|
||||
|
||||
byte[] getLatestData();
|
||||
|
||||
short getUsagePage();
|
||||
short getUsage();
|
||||
|
||||
String getPath();
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ class PureJavaHidDevice implements HidDevice, InputReportListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getUsagePage() {
|
||||
public short getUsage() {
|
||||
return myDeviceInfo.getUsagePage();
|
||||
}
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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<String> toRemove = new ArrayList<String>();
|
||||
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<String, ControllerType> detectWindowsControllers() {
|
||||
private static Map<String, ControllerType> detectXInputControllers() {
|
||||
Map<String, ControllerType> result = new HashMap<String, ControllerType>();
|
||||
ControllerType type = ControllerType.XINPUT13;
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user