removed the HidDeviceInfo class and added more methods to the HidDevice interface

(still untested)
This commit is contained in:
Maschell 2017-04-05 18:14:58 +02:00
parent e71d760cde
commit 367d2c79b6
12 changed files with 78 additions and 186 deletions

View File

@ -43,7 +43,6 @@ public class HidController extends Controller {
if (device != null) {
vid = device.getVendorId();
pid = device.getProductId();
device.close();
}
// We use a special version to optimize the data for the switch pro controller
@ -62,10 +61,9 @@ public class HidController extends Controller {
@Override
public boolean initController(String identifier) {
HidDevice device;
try {
device = HidManager.getDeviceByPath(identifier);
if (device == null) {
HidDevice device = HidManager.getDeviceByPath(identifier);
if (device == null || !device.open()) {
return false;
}
@ -108,7 +106,7 @@ public class HidController extends Controller {
@Override
public String getInfoText() {
// TODO:
// TODO: own class for joycons
if (getVID() == 0x57e) {
if (getPID() == 0x2006) {
return "Joy-Con (L) on " + getIdentifier();

View File

@ -31,4 +31,10 @@ public interface HidDevice {
byte[] getLatestData();
short getUsagePage();
String getPath();
boolean open();
}

View File

@ -1,33 +0,0 @@
/*******************************************************************************
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*******************************************************************************/
package net.ash.HIDToVPADNetworkClient.hid;
public interface HidDeviceInfo {
public short getUsagePage();
public int getVendorId();
public int getProductId();
public String getPath();
}

View File

@ -31,7 +31,7 @@ import net.ash.HIDToVPADNetworkClient.util.Settings;
public class HidManager {
private final static HidManagerBackend backend;
public static List<HidDeviceInfo> getAttachedController() {
public static List<HidDevice> getAttachedController() {
return backend.getAttachedController();
}

View File

@ -38,10 +38,10 @@ public abstract class HidManagerBackend {
*/
public abstract HidDevice getDeviceByPath(String path) throws IOException;
public List<HidDeviceInfo> getAttachedController() {
List<HidDeviceInfo> connectedGamepads = new ArrayList<HidDeviceInfo>();
public List<HidDevice> getAttachedController() {
List<HidDevice> connectedGamepads = new ArrayList<HidDevice>();
for (HidDeviceInfo info : enumerateDevices()) {
for (HidDevice info : enumerateDevices()) {
if (isGamepad(info)) {
// Skip Xbox controller under windows. We should use XInput instead.
if (isXboxController(info) && Settings.isWindows()) {
@ -53,26 +53,26 @@ public abstract class HidManagerBackend {
return connectedGamepads;
}
public static boolean isGamepad(HidDeviceInfo info) {
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));
}
private static boolean isPlaystationController(HidDeviceInfo info) {
private static boolean isPlaystationController(HidDevice info) {
if (info == null) return false;
return (info.getVendorId() == 0x054c);
}
private static boolean isNintendoController(HidDeviceInfo info) {
private static boolean isNintendoController(HidDevice info) {
if (info == null) return false;
return (info.getVendorId() == 0x57e);
}
private static boolean isXboxController(HidDeviceInfo info) {
private static boolean isXboxController(HidDevice info) {
if (info == null) return false;
return (info.getVendorId() == 0x045e) && ((info.getProductId() == 0x02ff) || (info.getProductId() == 0x02a1));
}
public abstract List<HidDeviceInfo> enumerateDevices();
public abstract List<HidDevice> enumerateDevices();
}

View File

@ -34,6 +34,16 @@ class Hid4JavaHidDevice implements HidDevice {
this.myDevice = device;
}
@Override
public boolean open() {
return myDevice.open();
}
@Override
public void close() {
myDevice.close();
}
@Override
public short getVendorId() {
return myDevice.getVendorId();
@ -44,15 +54,21 @@ class Hid4JavaHidDevice implements HidDevice {
return myDevice.getProductId();
}
@Override
public void close() {
myDevice.close();
}
@Override
public byte[] getLatestData() {
int length = myDevice.read(data);
if (length <= 0) return null;
return Arrays.copyOf(data, length);
}
@Override
public short getUsagePage() {
return (short) myDevice.getUsagePage();
}
@Override
public String getPath() {
return myDevice.getPath();
}
}

View File

@ -1,54 +0,0 @@
/*******************************************************************************
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*******************************************************************************/
package net.ash.HIDToVPADNetworkClient.hid.hid4java;
import org.hid4java.HidDevice;
import net.ash.HIDToVPADNetworkClient.hid.HidDeviceInfo;
class Hid4JavaHidDeviceInfo implements HidDeviceInfo {
private final HidDevice myDevice;
public Hid4JavaHidDeviceInfo(HidDevice device) {
myDevice = device;
}
@Override
public short getUsagePage() {
return (short) myDevice.getUsagePage();
}
@Override
public int getVendorId() {
return myDevice.getVendorId();
}
@Override
public int getProductId() {
return myDevice.getProductId();
}
@Override
public String getPath() {
return myDevice.getPath();
}
}

View File

@ -29,7 +29,6 @@ import org.hid4java.HidManager;
import org.hid4java.HidServices;
import net.ash.HIDToVPADNetworkClient.hid.HidDevice;
import net.ash.HIDToVPADNetworkClient.hid.HidDeviceInfo;
import net.ash.HIDToVPADNetworkClient.hid.HidManagerBackend;
public class Hid4JavaHidManagerBackend extends HidManagerBackend {
@ -50,10 +49,10 @@ public class Hid4JavaHidManagerBackend extends HidManagerBackend {
}
@Override
public List<HidDeviceInfo> enumerateDevices() {
List<HidDeviceInfo> result = new ArrayList<HidDeviceInfo>();
public List<HidDevice> enumerateDevices() {
List<HidDevice> result = new ArrayList<HidDevice>();
for (org.hid4java.HidDevice info : HidManager.getHidServices().getAttachedHidDevices()) {
result.add(new Hid4JavaHidDeviceInfo(info));
result.add(new Hid4JavaHidDevice(info));
}
return result;
}

View File

@ -21,21 +21,23 @@
*******************************************************************************/
package net.ash.HIDToVPADNetworkClient.hid.purejavahid;
import java.io.IOException;
import java.util.Arrays;
import lombok.Synchronized;
import net.ash.HIDToVPADNetworkClient.hid.HidDevice;
import purejavahidapi.HidDeviceInfo;
import purejavahidapi.InputReportListener;
class PureJavaHidDevice implements HidDevice, InputReportListener {
private final purejavahidapi.HidDevice myDevice;
private purejavahidapi.HidDevice myDevice = null;
private final purejavahidapi.HidDeviceInfo myDeviceInfo;
private final Object dataLock = new Object();
protected byte[] currentData = new byte[1];
public PureJavaHidDevice(purejavahidapi.HidDevice device) {
this.myDevice = device;
device.setInputReportListener(this);
public PureJavaHidDevice(HidDeviceInfo info) {
this.myDeviceInfo = info;
}
@Override
@ -46,12 +48,25 @@ class PureJavaHidDevice implements HidDevice, InputReportListener {
@Override
public short getVendorId() {
return myDevice.getHidDeviceInfo().getVendorId();
return myDeviceInfo.getVendorId();
}
@Override
public short getProductId() {
return myDevice.getHidDeviceInfo().getProductId();
return myDeviceInfo.getProductId();
}
@Override
public boolean open() {
boolean result = true;
try {
myDevice = purejavahidapi.PureJavaHidApi.openDevice(myDeviceInfo);
myDevice.setInputReportListener(this);
} catch (IOException e) {
result = false;
e.printStackTrace();
}
return result;
}
@Override
@ -65,4 +80,13 @@ class PureJavaHidDevice implements HidDevice, InputReportListener {
return currentData.clone();
}
@Override
public short getUsagePage() {
return myDeviceInfo.getUsagePage();
}
@Override
public String getPath() {
return myDeviceInfo.getPath();
}
}

View File

@ -1,53 +0,0 @@
/*******************************************************************************
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*******************************************************************************/
package net.ash.HIDToVPADNetworkClient.hid.purejavahid;
import net.ash.HIDToVPADNetworkClient.hid.HidDeviceInfo;
class PureJavaHidDeviceInfo implements HidDeviceInfo {
private final purejavahidapi.HidDeviceInfo myInfo;
public PureJavaHidDeviceInfo(purejavahidapi.HidDeviceInfo info) {
this.myInfo = info;
}
@Override
public short getUsagePage() {
return myInfo.getUsagePage();
}
@Override
public int getVendorId() {
return myInfo.getVendorId();
}
@Override
public int getProductId() {
return myInfo.getProductId();
}
@Override
public String getPath() {
return myInfo.getPath();
}
}

View File

@ -26,17 +26,16 @@ import java.util.ArrayList;
import java.util.List;
import net.ash.HIDToVPADNetworkClient.hid.HidDevice;
import net.ash.HIDToVPADNetworkClient.hid.HidDeviceInfo;
import net.ash.HIDToVPADNetworkClient.hid.HidManagerBackend;
import purejavahidapi.PureJavaHidApi;
public class PureJavaHidManagerBackend extends HidManagerBackend {
@Override
public List<HidDeviceInfo> enumerateDevices() {
List<HidDeviceInfo> result = new ArrayList<HidDeviceInfo>();
public List<HidDevice> enumerateDevices() {
List<HidDevice> result = new ArrayList<HidDevice>();
for (purejavahidapi.HidDeviceInfo info : PureJavaHidApi.enumerateDevices()) {
result.add(new PureJavaHidDeviceInfo(info));
result.add(new PureJavaHidDevice(info));
}
return result;
}
@ -48,7 +47,7 @@ public class PureJavaHidManagerBackend extends HidManagerBackend {
for (purejavahidapi.HidDeviceInfo info : devList) {
String real_path = info.getPath();
if (real_path.equals(path)) {
return new PureJavaHidDevice(PureJavaHidApi.openDevice(info));
return new PureJavaHidDevice(info);
}
}

View File

@ -44,7 +44,7 @@ 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.hid.HidDeviceInfo;
import net.ash.HIDToVPADNetworkClient.hid.HidDevice;
import net.ash.HIDToVPADNetworkClient.hid.HidManager;
import net.ash.HIDToVPADNetworkClient.util.MessageBox;
import net.ash.HIDToVPADNetworkClient.util.MessageBoxManager;
@ -53,7 +53,6 @@ import net.ash.HIDToVPADNetworkClient.util.Settings;
@Log
public final class ControllerManager {
private static final Map<String, Controller> attachedControllers = new HashMap<String, Controller>();
private static final Map<String, HidDeviceInfo> connectedDevicesInfo = new HashMap<String, HidDeviceInfo>();
private static boolean threwUnsatisfiedLinkError = false;
@ -157,12 +156,9 @@ public final class ControllerManager {
private static Map<String, ControllerType> detectHIDDevices() {
Map<String, ControllerType> connectedDevices = new HashMap<String, ControllerType>();
for (HidDeviceInfo info : HidManager.getAttachedController()) {
for (HidDevice info : HidManager.getAttachedController()) {
String path = info.getPath();
connectedDevices.put(path, ControllerType.HIDController);
synchronized (connectedDevicesInfo) {
connectedDevicesInfo.put(path, info);
}
}
return connectedDevices;
@ -244,10 +240,4 @@ public final class ControllerManager {
c.setActive(false);
}
}
@Synchronized("connectedDevicesInfo")
public static HidDeviceInfo getDeviceInfoByPath(String path) {
return connectedDevicesInfo.get(path);
}
}