Refactoring OS detection and OSX handling

This commit is contained in:
Maschell 2017-03-23 22:56:32 +01:00
parent 1e5db6e56f
commit 316f5366ee
12 changed files with 113 additions and 124 deletions

View File

@ -235,7 +235,7 @@
<setting id="org.eclipse.jdt.core.formatter.comment.format_block_comments" value="true"/> <setting id="org.eclipse.jdt.core.formatter.comment.format_block_comments" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line" value="false"/> <setting id="org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration" value="16"/> <setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference" value="do not insert"/>

View File

@ -140,8 +140,7 @@ public abstract class Controller implements Runnable {
done = shutdownDone; done = shutdownDone;
} }
Utilities.sleep(50); Utilities.sleep(50);
if (i++ > 50) if (i++ > 50) System.out.println("Thread doesn't stop!!");
System.out.println("Thread doesn't stop!!");
} }
} }
}).start(); }).start();
@ -176,20 +175,14 @@ public abstract class Controller implements Runnable {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) return true;
return true; if (obj == null) return false;
if (obj == null) if (getClass() != obj.getClass()) return false;
return false;
if (getClass() != obj.getClass())
return false;
Controller other = (Controller) obj; Controller other = (Controller) obj;
if (identifier == null) { if (identifier == null) {
if (other.identifier != null) if (other.identifier != null) return false;
return false; } else if (!identifier.equals(other.identifier)) return false;
} else if (!identifier.equals(other.identifier)) if (type != other.type) return false;
return false;
if (type != other.type)
return false;
return true; return true;
} }

View File

@ -54,8 +54,7 @@ public class XInputController extends Controller {
} catch (XInputNotLoadedException e) { } catch (XInputNotLoadedException e) {
// TODO: Log? // TODO: Log?
} }
if (device == null) if (device == null) return false;
return false;
setDevice(device); setDevice(device);
return true; return true;
} }
@ -69,41 +68,25 @@ public class XInputController extends Controller {
XInputButtons buttons = components.getButtons(); XInputButtons buttons = components.getButtons();
int buttonState = 0; int buttonState = 0;
if (buttons.a) if (buttons.a) buttonState |= (1 << 0);
buttonState |= (1 << 0); if (buttons.b) buttonState |= (1 << 1);
if (buttons.b) if (buttons.x) buttonState |= (1 << 2);
buttonState |= (1 << 1); if (buttons.y) buttonState |= (1 << 3);
if (buttons.x)
buttonState |= (1 << 2);
if (buttons.y)
buttonState |= (1 << 3);
if (buttons.left) if (buttons.left) buttonState |= (1 << 4);
buttonState |= (1 << 4); if (buttons.up) buttonState |= (1 << 5);
if (buttons.up) if (buttons.right) buttonState |= (1 << 6);
buttonState |= (1 << 5); if (buttons.down) buttonState |= (1 << 7);
if (buttons.right)
buttonState |= (1 << 6);
if (buttons.down)
buttonState |= (1 << 7);
if (buttons.back) if (buttons.back) buttonState |= (1 << 8);
buttonState |= (1 << 8); if (buttons.start) buttonState |= (1 << 9);
if (buttons.start) if (buttons.lShoulder) buttonState |= (1 << 10);
buttonState |= (1 << 9); if (buttons.rShoulder) buttonState |= (1 << 11);
if (buttons.lShoulder) if (buttons.lThumb) buttonState |= (1 << 12);
buttonState |= (1 << 10); if (buttons.rThumb) buttonState |= (1 << 13);
if (buttons.rShoulder) if (buttons.unknown) buttonState |= (1 << 14);
buttonState |= (1 << 11);
if (buttons.lThumb)
buttonState |= (1 << 12);
if (buttons.rThumb)
buttonState |= (1 << 13);
if (buttons.unknown)
buttonState |= (1 << 14);
if (XInputDevice.isGuideButtonSupported()) { if (XInputDevice.isGuideButtonSupported()) {
if (buttons.guide) if (buttons.guide) buttonState |= (1 << 15);
buttonState |= (1 << 15);
} }
XInputAxes axes = components.getAxes(); XInputAxes axes = components.getAxes();

View File

@ -85,18 +85,13 @@ public class GuiControllerListItem extends JPanel implements ActionListener {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) return true;
return true; if (obj == null) return false;
if (obj == null) if (getClass() != obj.getClass()) return false;
return false;
if (getClass() != obj.getClass())
return false;
GuiControllerListItem other = (GuiControllerListItem) obj; GuiControllerListItem other = (GuiControllerListItem) obj;
if (controller == null) { if (controller == null) {
if (other.controller != null) if (other.controller != null) return false;
return false; } else if (!controller.equals(other.controller)) return false;
} else if (!controller.equals(other.controller))
return false;
return true; return true;
} }
} }

View File

@ -37,15 +37,15 @@ import com.ivan.xinput.exceptions.XInputNotLoadedException;
import lombok.Synchronized; import lombok.Synchronized;
import net.ash.HIDToVPADNetworkClient.controller.Controller; import net.ash.HIDToVPADNetworkClient.controller.Controller;
import net.ash.HIDToVPADNetworkClient.controller.Controller.ControllerType; import net.ash.HIDToVPADNetworkClient.controller.Controller.ControllerType;
import net.ash.HIDToVPADNetworkClient.controller.PureJavaHidController;
import net.ash.HIDToVPADNetworkClient.controller.LinuxDevInputController; import net.ash.HIDToVPADNetworkClient.controller.LinuxDevInputController;
import net.ash.HIDToVPADNetworkClient.controller.PureJavaHidController;
import net.ash.HIDToVPADNetworkClient.controller.XInput13Controller; import net.ash.HIDToVPADNetworkClient.controller.XInput13Controller;
import net.ash.HIDToVPADNetworkClient.controller.XInput14Controller; import net.ash.HIDToVPADNetworkClient.controller.XInput14Controller;
import net.ash.HIDToVPADNetworkClient.controller.XInputController; import net.ash.HIDToVPADNetworkClient.controller.XInputController;
import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException; import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException;
import net.ash.HIDToVPADNetworkClient.util.PureJavaHidApiManager; import net.ash.HIDToVPADNetworkClient.util.PureJavaHidApiManager;
import net.ash.HIDToVPADNetworkClient.util.Settings;
import purejavahidapi.HidDeviceInfo; import purejavahidapi.HidDeviceInfo;
import purejavahidapi.PureJavaHidApi;
public class ControllerManager { public class ControllerManager {
private static Map<String, Controller> attachedControllers = new HashMap<String, Controller>(); private static Map<String, Controller> attachedControllers = new HashMap<String, Controller>();
@ -55,24 +55,15 @@ public class ControllerManager {
*/ */
@Synchronized("attachedControllers") @Synchronized("attachedControllers")
public static void detectControllers() { public static void detectControllers() {
String os = System.getProperty("os.name");
// System.out.println("[ControllerDetector] OS: " + os);
Map<String, ControllerType> connectedDevices = new HashMap<String, ControllerType>(); Map<String, ControllerType> connectedDevices = new HashMap<String, ControllerType>();
if (os.contains("Linux")) { if (Settings.isLinux()) {
connectedDevices.putAll(detectLinuxControllers()); connectedDevices.putAll(detectLinuxControllers());
} else if (os.contains("Windows")) { } else if (Settings.isWindows()) {
connectedDevices.putAll(detectWindowsControllers()); connectedDevices.putAll(detectWindowsControllers());
} }
if (os.contains("Mac OS X")) { connectedDevices.putAll(detectHIDDevices());
connectedDevices.putAll(detectOSXHIDDevices());
PureJavaHidApiManager.MAC_OS_X = true;
} else {
connectedDevices.putAll(detectHIDDevices());
PureJavaHidApiManager.MAC_OS_X = false;
}
// Remove detached devices // Remove detached devices
List<String> toRemove = new ArrayList<String>(); List<String> toRemove = new ArrayList<String>();
@ -147,22 +138,11 @@ public class ControllerManager {
private static Map<String, ControllerType> detectHIDDevices() { private static Map<String, ControllerType> detectHIDDevices() {
Map<String, ControllerType> connectedDevices = new HashMap<String, ControllerType>(); Map<String, ControllerType> connectedDevices = new HashMap<String, ControllerType>();
for (HidDeviceInfo info : PureJavaHidApi.enumerateDevices()) { for (HidDeviceInfo info : PureJavaHidApiManager.getAttachedController()) {
if (info.getUsagePage() == 0x05 || info.getUsagePage() == 0x04 || (info.getVendorId() == 0x57e) || (info.getVendorId() == 0x054c)) { String path = info.getPath();
connectedDevices.put(info.getPath(), ControllerType.PureJAVAHid);
}
}
return connectedDevices; if (Settings.isMacOSX()) path = path.substring(0, 13);
} connectedDevices.put(path, ControllerType.PureJAVAHid);
private static Map<String, ControllerType> detectOSXHIDDevices() {
Map<String, ControllerType> connectedDevices = new HashMap<String, ControllerType>();
for (HidDeviceInfo info : PureJavaHidApi.enumerateDevices()) {
if (info.getUsagePage() == 0x05 || info.getUsagePage() == 0x04 || (info.getVendorId() == 0x57e) || (info.getVendorId() == 0x054c)) {
connectedDevices.put(info.getPath().substring(0, 13), ControllerType.PureJAVAHid);
}
} }
return connectedDevices; return connectedDevices;
@ -179,10 +159,7 @@ public class ControllerManager {
XInputDevice device; XInputDevice device;
try { try {
device = XInputDevice.getDeviceFor(i); device = XInputDevice.getDeviceFor(i);
if (device.poll() && device.isConnected()) { // Check if it if (device.poll() && device.isConnected()) { // Check if it is this controller is connected
// is this
// controller
// is connected
result.put(XInputController.XINPUT_INDENTIFER + i, type); result.put(XInputController.XINPUT_INDENTIFER + i, type);
} }
} catch (XInputNotLoadedException e) { } catch (XInputNotLoadedException e) {
@ -198,8 +175,7 @@ public class ControllerManager {
private static Map<String, ControllerType> detectLinuxControllers() { private static Map<String, ControllerType> detectLinuxControllers() {
Map<String, ControllerType> result = new HashMap<String, ControllerType>(); Map<String, ControllerType> result = new HashMap<String, ControllerType>();
File devInput = new File("/dev/input"); File devInput = new File("/dev/input");
if (!devInput.exists()) if (!devInput.exists()) return result;
return result;
File[] linuxControllers = devInput.listFiles(new FilenameFilter() { File[] linuxControllers = devInput.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) { public boolean accept(File dir, String name) {

View File

@ -108,15 +108,11 @@ public class NetworkHIDDevice {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) return true;
return true; if (obj == null) return false;
if (obj == null) if (getClass() != obj.getClass()) return false;
return false;
if (getClass() != obj.getClass())
return false;
NetworkHIDDevice other = (NetworkHIDDevice) obj; NetworkHIDDevice other = (NetworkHIDDevice) obj;
if (hidHandle != other.hidHandle) if (hidHandle != other.hidHandle) return false;
return false;
return true; return true;
} }
} }

View File

@ -92,8 +92,7 @@ public class NetworkManager implements Runnable {
} }
private void ping() { private void ping() {
if (isConnected() || tcpClient.isShouldRetry()) if (isConnected() || tcpClient.isShouldRetry()) sendingCommand(new PingCommand());
sendingCommand(new PingCommand());
} }
public void proccessCommands() { public void proccessCommands() {
@ -110,8 +109,7 @@ public class NetworkManager implements Runnable {
} }
} }
if (commands.isEmpty()) if (commands.isEmpty()) return;
return;
// Split up into "read commands" and other commands. // Split up into "read commands" and other commands.
List<ReadCommand> readCommands = new ArrayList<ReadCommand>(); List<ReadCommand> readCommands = new ArrayList<ReadCommand>();

View File

@ -78,8 +78,7 @@ public class TCPClient {
} }
private synchronized HandshakeReturnCode doHandshake() throws Exception { private synchronized HandshakeReturnCode doHandshake() throws Exception {
if (recvByte() != Protocol.TCP_HANDSHAKE) if (recvByte() != Protocol.TCP_HANDSHAKE) return HandshakeReturnCode.BAD_HANDSHAKE;
return HandshakeReturnCode.BAD_HANDSHAKE;
send(clientID); send(clientID);
log.info("[TCP] Handshaking..."); log.info("[TCP] Handshaking...");
HandshakeReturnCode test = (recvByte() == Protocol.TCP_NEW_CLIENT) ? HandshakeReturnCode.NEW_CLIENT : HandshakeReturnCode.SAME_CLIENT; HandshakeReturnCode test = (recvByte() == Protocol.TCP_NEW_CLIENT) ? HandshakeReturnCode.NEW_CLIENT : HandshakeReturnCode.SAME_CLIENT;

View File

@ -28,9 +28,8 @@ package net.ash.HIDToVPADNetworkClient.util;
import java.util.Random; import java.util.Random;
public class HandleFoundry { public class HandleFoundry {
// We start with a random value, so we have at each startup a different // We start with a random value, so we have at each startup a different clientID!
// clientID! private static int h = new Random().nextInt() % 10000;
private static int h = new Random().nextInt();
private HandleFoundry() { private HandleFoundry() {
} }

View File

@ -22,6 +22,7 @@
package net.ash.HIDToVPADNetworkClient.util; package net.ash.HIDToVPADNetworkClient.util;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import purejavahidapi.HidDevice; import purejavahidapi.HidDevice;
@ -33,8 +34,6 @@ public class PureJavaHidApiManager {
private PureJavaHidApiManager() { private PureJavaHidApiManager() {
} }
public static boolean MAC_OS_X;
/** /**
* Searches the corresponding HIDDevice for the given path * Searches the corresponding HIDDevice for the given path
* *
@ -45,17 +44,36 @@ public class PureJavaHidApiManager {
*/ */
public static HidDevice getDeviceByPath(String path) throws IOException { public static HidDevice getDeviceByPath(String path) throws IOException {
List<HidDeviceInfo> devList = PureJavaHidApi.enumerateDevices(); List<HidDeviceInfo> devList = PureJavaHidApi.enumerateDevices();
HidDevice result = null;
for (HidDeviceInfo info : devList) { for (HidDeviceInfo info : devList) {
if (MAC_OS_X) { result = openDeviceByPath(info, path);
if (info.getPath().substring(0, 13).equals(path)) { if (result != null) return result;
return PureJavaHidApi.openDevice(info);
}
} else {
if (info.getPath().equals(path)) {
return PureJavaHidApi.openDevice(info);
}
}
} }
return result;
}
private static HidDevice openDeviceByPath(HidDeviceInfo info, String expected_path) throws IOException {
if (info == null) return null;
String real_path = info.getPath();
if (Settings.isMacOSX()) real_path = real_path.substring(0, 13);
if (real_path.equals(expected_path)){
return PureJavaHidApi.openDevice(info);
}
return null; return null;
} }
public static List<HidDeviceInfo> getAttachedController() {
List<HidDeviceInfo> connectedGamepads = new ArrayList<HidDeviceInfo>();
for (HidDeviceInfo info : PureJavaHidApi.enumerateDevices()) {
if (info.getUsagePage() == 0x05 || info.getUsagePage() == 0x04 || (info.getVendorId() == 0x57e) || (info.getVendorId() == 0x054c)) {
connectedGamepads.add(info);
}
}
return connectedGamepads;
}
} }

View File

@ -26,11 +26,15 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.java.Log; import lombok.extern.java.Log;
import net.ash.HIDToVPADNetworkClient.controller.Controller.ControllerType;
import net.ash.HIDToVPADNetworkClient.util.Settings.Platform;
//TODO autosave IP addr //TODO autosave IP addr
@ -125,4 +129,33 @@ public class Settings {
private static String getConfigDir() { private static String getConfigDir() {
return "config/"; return "config/";
} }
public static boolean isLinux() {
return getPlattform() == Platform.LINUX;
}
public static boolean isWindows() {
return getPlattform() == Platform.WINDOWS;
}
public static boolean isMacOSX() {
return getPlattform() == Platform.MAC_OS_X;
}
public static Platform getPlattform() {
String os = System.getProperty("os.name");
if (os.contains("Linux")) {
return Platform.LINUX;
} else if (os.contains("Windows")) {
return Platform.WINDOWS;
} else if (os.contains("Mac OS X")) {
return Platform.MAC_OS_X;
}
return null;
}
public enum Platform {
LINUX, WINDOWS, MAC_OS_X, UNKNOWN
}
} }

View File

@ -47,8 +47,7 @@ public class Utilities {
* @return String representing the binary data * @return String representing the binary data
*/ */
public static String ByteArrayToString(byte[] ba) { public static String ByteArrayToString(byte[] ba) {
if (ba == null) if (ba == null) return null;
return null;
StringBuilder hex = new StringBuilder(ba.length * 2); StringBuilder hex = new StringBuilder(ba.length * 2);
for (byte b : ba) { for (byte b : ba) {
hex.append(String.format("%02X", b)); hex.append(String.format("%02X", b));