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.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.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.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"/>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -78,8 +78,7 @@ public class TCPClient {
}
private synchronized HandshakeReturnCode doHandshake() throws Exception {
if (recvByte() != Protocol.TCP_HANDSHAKE)
return HandshakeReturnCode.BAD_HANDSHAKE;
if (recvByte() != Protocol.TCP_HANDSHAKE) return HandshakeReturnCode.BAD_HANDSHAKE;
send(clientID);
log.info("[TCP] Handshaking...");
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;
public class HandleFoundry {
// We start with a random value, so we have at each startup a different
// clientID!
private static int h = new Random().nextInt();
// We start with a random value, so we have at each startup a different clientID!
private static int h = new Random().nextInt() % 10000;
private HandleFoundry() {
}

View File

@ -22,6 +22,7 @@
package net.ash.HIDToVPADNetworkClient.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import purejavahidapi.HidDevice;
@ -33,8 +34,6 @@ public class PureJavaHidApiManager {
private PureJavaHidApiManager() {
}
public static boolean MAC_OS_X;
/**
* Searches the corresponding HIDDevice for the given path
*
@ -45,17 +44,36 @@ public class PureJavaHidApiManager {
*/
public static HidDevice getDeviceByPath(String path) throws IOException {
List<HidDeviceInfo> devList = PureJavaHidApi.enumerateDevices();
HidDevice result = null;
for (HidDeviceInfo info : devList) {
if (MAC_OS_X) {
if (info.getPath().substring(0, 13).equals(path)) {
result = openDeviceByPath(info, path);
if (result != null) return result;
}
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);
}
} else {
if (info.getPath().equals(path)) {
return PureJavaHidApi.openDevice(info);
}
}
}
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.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.java.Log;
import net.ash.HIDToVPADNetworkClient.controller.Controller.ControllerType;
import net.ash.HIDToVPADNetworkClient.util.Settings.Platform;
//TODO autosave IP addr
@ -125,4 +129,33 @@ public class Settings {
private static String getConfigDir() {
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
*/
public static String ByteArrayToString(byte[] ba) {
if (ba == null)
return null;
if (ba == null) return null;
StringBuilder hex = new StringBuilder(ba.length * 2);
for (byte b : ba) {
hex.append(String.format("%02X", b));