Skip to content

Commit 9e18a68

Browse files
committed
android: updated SDL files
1 parent 36d61ce commit 9e18a68

File tree

5 files changed

+273
-145
lines changed

5 files changed

+273
-145
lines changed

android/app/src/main/java/org/libsdl/app/HIDDevice.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.libsdl.app;
22

3+
import android.hardware.usb.UsbDevice;
4+
35
interface HIDDevice
46
{
57
public int getId();
@@ -9,6 +11,7 @@ interface HIDDevice
911
public int getVersion();
1012
public String getManufacturerName();
1113
public String getProductName();
14+
public UsbDevice getDevice();
1215
public boolean open();
1316
public int sendFeatureReport(byte[] report);
1417
public int sendOutputReport(byte[] report);

android/app/src/main/java/org/libsdl/app/HIDDeviceBLESteamController.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.bluetooth.BluetoothManager;
1010
import android.bluetooth.BluetoothProfile;
1111
import android.bluetooth.BluetoothGattService;
12+
import android.hardware.usb.UsbDevice;
1213
import android.os.Handler;
1314
import android.os.Looper;
1415
import android.util.Log;
@@ -165,13 +166,13 @@ public HIDDeviceBLESteamController(HIDDeviceManager manager, BluetoothDevice dev
165166
mHandler = new Handler(Looper.getMainLooper());
166167

167168
mGatt = connectGatt();
168-
final HIDDeviceBLESteamController finalThis = this;
169-
mHandler.postDelayed(new Runnable() {
170-
@Override
171-
public void run() {
172-
finalThis.checkConnectionForChromebookIssue();
173-
}
174-
}, CHROMEBOOK_CONNECTION_CHECK_INTERVAL);
169+
// final HIDDeviceBLESteamController finalThis = this;
170+
// mHandler.postDelayed(new Runnable() {
171+
// @Override
172+
// public void run() {
173+
// finalThis.checkConnectionForChromebookIssue();
174+
// }
175+
// }, CHROMEBOOK_CONNECTION_CHECK_INTERVAL);
175176
}
176177

177178
public String getIdentifier() {
@@ -469,7 +470,7 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi
469470
// Only register controller with the native side once it has been fully configured
470471
if (!isRegistered()) {
471472
Log.v(TAG, "Registering Steam Controller with ID: " + getId());
472-
mManager.HIDDeviceConnected(getId(), getIdentifier(), getVendorId(), getProductId(), getSerialNumber(), getVersion(), getManufacturerName(), getProductName(), 0);
473+
mManager.HIDDeviceConnected(getId(), getIdentifier(), getVendorId(), getProductId(), getSerialNumber(), getVersion(), getManufacturerName(), getProductName(), 0, 0, 0, 0);
473474
setRegistered();
474475
}
475476
}
@@ -563,6 +564,11 @@ public String getProductName() {
563564
return "Steam Controller";
564565
}
565566

567+
@Override
568+
public UsbDevice getDevice() {
569+
return null;
570+
}
571+
566572
@Override
567573
public boolean open() {
568574
return true;

android/app/src/main/java/org/libsdl/app/HIDDeviceManager.java

+61-74
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
import android.os.Handler;
2020
import android.os.Looper;
2121

22-
import java.util.HashMap;
2322
import java.util.ArrayList;
23+
import java.util.HashMap;
24+
import java.util.Iterator;
2425
import java.util.List;
2526

2627
public class HIDDeviceManager {
@@ -50,7 +51,6 @@ public static void release(HIDDeviceManager manager) {
5051

5152
private Context mContext;
5253
private HashMap<Integer, HIDDevice> mDevicesById = new HashMap<Integer, HIDDevice>();
53-
private HashMap<UsbDevice, HIDDeviceUSB> mUSBDevices = new HashMap<UsbDevice, HIDDeviceUSB>();
5454
private HashMap<BluetoothDevice, HIDDeviceBLESteamController> mBluetoothDevices = new HashMap<BluetoothDevice, HIDDeviceBLESteamController>();
5555
private int mNextDeviceId = 0;
5656
private SharedPreferences mSharedPreferences = null;
@@ -241,31 +241,20 @@ private void shutdownUSB() {
241241
}
242242
}
243243

244-
private boolean isHIDDeviceUSB(UsbDevice usbDevice) {
245-
for (int interface_number = 0; interface_number < usbDevice.getInterfaceCount(); ++interface_number) {
246-
if (isHIDDeviceInterface(usbDevice, interface_number)) {
247-
return true;
248-
}
249-
}
250-
return false;
251-
}
252-
253-
private boolean isHIDDeviceInterface(UsbDevice usbDevice, int interface_number) {
254-
UsbInterface usbInterface = usbDevice.getInterface(interface_number);
244+
private boolean isHIDDeviceInterface(UsbDevice usbDevice, UsbInterface usbInterface) {
255245
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_HID) {
256246
return true;
257247
}
258-
if (interface_number == 0) {
259-
if (isXbox360Controller(usbDevice, usbInterface) || isXboxOneController(usbDevice, usbInterface)) {
260-
return true;
261-
}
248+
if (isXbox360Controller(usbDevice, usbInterface) || isXboxOneController(usbDevice, usbInterface)) {
249+
return true;
262250
}
263251
return false;
264252
}
265253

266254
private boolean isXbox360Controller(UsbDevice usbDevice, UsbInterface usbInterface) {
267255
final int XB360_IFACE_SUBCLASS = 93;
268-
final int XB360_IFACE_PROTOCOL = 1; // Wired only
256+
final int XB360_IFACE_PROTOCOL = 1; // Wired
257+
final int XB360W_IFACE_PROTOCOL = 129; // Wireless
269258
final int[] SUPPORTED_VENDORS = {
270259
0x0079, // GPD Win 2
271260
0x044f, // Thrustmaster
@@ -275,8 +264,9 @@ private boolean isXbox360Controller(UsbDevice usbDevice, UsbInterface usbInterfa
275264
0x06a3, // Saitek
276265
0x0738, // Mad Catz
277266
0x07ff, // Mad Catz
278-
0x0e6f, // Unknown
267+
0x0e6f, // PDP
279268
0x0f0d, // Hori
269+
0x1038, // SteelSeries
280270
0x11c9, // Nacon
281271
0x12ab, // Unknown
282272
0x1430, // RedOctane
@@ -291,7 +281,8 @@ private boolean isXbox360Controller(UsbDevice usbDevice, UsbInterface usbInterfa
291281

292282
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC &&
293283
usbInterface.getInterfaceSubclass() == XB360_IFACE_SUBCLASS &&
294-
usbInterface.getInterfaceProtocol() == XB360_IFACE_PROTOCOL) {
284+
(usbInterface.getInterfaceProtocol() == XB360_IFACE_PROTOCOL ||
285+
usbInterface.getInterfaceProtocol() == XB360W_IFACE_PROTOCOL)) {
295286
int vendor_id = usbDevice.getVendorId();
296287
for (int supportedVid : SUPPORTED_VENDORS) {
297288
if (vendor_id == supportedVid) {
@@ -308,13 +299,15 @@ private boolean isXboxOneController(UsbDevice usbDevice, UsbInterface usbInterfa
308299
final int[] SUPPORTED_VENDORS = {
309300
0x045e, // Microsoft
310301
0x0738, // Mad Catz
311-
0x0e6f, // Unknown
302+
0x0e6f, // PDP
312303
0x0f0d, // Hori
313304
0x1532, // Razer Wildcat
314305
0x24c6, // PowerA
306+
0x2e24, // Hyperkin
315307
};
316308

317-
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC &&
309+
if (usbInterface.getId() == 0 &&
310+
usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC &&
318311
usbInterface.getInterfaceSubclass() == XB1_IFACE_SUBCLASS &&
319312
usbInterface.getInterfaceProtocol() == XB1_IFACE_PROTOCOL) {
320313
int vendor_id = usbDevice.getVendorId();
@@ -328,45 +321,45 @@ private boolean isXboxOneController(UsbDevice usbDevice, UsbInterface usbInterfa
328321
}
329322

330323
private void handleUsbDeviceAttached(UsbDevice usbDevice) {
331-
if (isHIDDeviceUSB(usbDevice)) {
332-
connectHIDDeviceUSB(usbDevice);
333-
}
324+
connectHIDDeviceUSB(usbDevice);
334325
}
335326

336327
private void handleUsbDeviceDetached(UsbDevice usbDevice) {
337-
HIDDeviceUSB device = mUSBDevices.get(usbDevice);
338-
if (device == null)
339-
return;
340-
341-
int id = device.getId();
342-
mUSBDevices.remove(usbDevice);
343-
mDevicesById.remove(id);
344-
device.shutdown();
345-
HIDDeviceDisconnected(id);
328+
List<Integer> devices = new ArrayList<Integer>();
329+
for (HIDDevice device : mDevicesById.values()) {
330+
if (usbDevice.equals(device.getDevice())) {
331+
devices.add(device.getId());
332+
}
333+
}
334+
for (int id : devices) {
335+
HIDDevice device = mDevicesById.get(id);
336+
mDevicesById.remove(id);
337+
device.shutdown();
338+
HIDDeviceDisconnected(id);
339+
}
346340
}
347341

348342
private void handleUsbDevicePermission(UsbDevice usbDevice, boolean permission_granted) {
349-
HIDDeviceUSB device = mUSBDevices.get(usbDevice);
350-
if (device == null)
351-
return;
352-
353-
boolean opened = false;
354-
if (permission_granted) {
355-
opened = device.open();
343+
for (HIDDevice device : mDevicesById.values()) {
344+
if (usbDevice.equals(device.getDevice())) {
345+
boolean opened = false;
346+
if (permission_granted) {
347+
opened = device.open();
348+
}
349+
HIDDeviceOpenResult(device.getId(), opened);
350+
}
356351
}
357-
HIDDeviceOpenResult(device.getId(), opened);
358352
}
359353

360354
private void connectHIDDeviceUSB(UsbDevice usbDevice) {
361355
synchronized (this) {
362-
for (int interface_number = 0; interface_number < usbDevice.getInterfaceCount(); interface_number++) {
363-
if (isHIDDeviceInterface(usbDevice, interface_number)) {
364-
HIDDeviceUSB device = new HIDDeviceUSB(this, usbDevice, interface_number);
356+
for (int interface_index = 0; interface_index < usbDevice.getInterfaceCount(); interface_index++) {
357+
UsbInterface usbInterface = usbDevice.getInterface(interface_index);
358+
if (isHIDDeviceInterface(usbDevice, usbInterface)) {
359+
HIDDeviceUSB device = new HIDDeviceUSB(this, usbDevice, interface_index);
365360
int id = device.getId();
366-
mUSBDevices.put(usbDevice, device);
367361
mDevicesById.put(id, device);
368-
HIDDeviceConnected(id, device.getIdentifier(), device.getVendorId(), device.getProductId(), device.getSerialNumber(), device.getVersion(), device.getManufacturerName(), device.getProductName(), interface_number);
369-
break;
362+
HIDDeviceConnected(id, device.getIdentifier(), device.getVendorId(), device.getProductId(), device.getSerialNumber(), device.getVersion(), device.getManufacturerName(), device.getProductName(), usbInterface.getId(), usbInterface.getInterfaceClass(), usbInterface.getInterfaceSubclass(), usbInterface.getInterfaceProtocol());
370363
}
371364
}
372365
}
@@ -563,33 +556,27 @@ private HIDDevice getDevice(int id) {
563556
//////////////////////////////////////////////////////////////////////////////////////////////////////
564557

565558
public boolean openDevice(int deviceID) {
559+
Log.v(TAG, "openDevice deviceID=" + deviceID);
560+
HIDDevice device = getDevice(deviceID);
561+
if (device == null) {
562+
HIDDeviceDisconnected(deviceID);
563+
return false;
564+
}
565+
566566
// Look to see if this is a USB device and we have permission to access it
567-
for (HIDDeviceUSB device : mUSBDevices.values()) {
568-
if (deviceID == device.getId()) {
569-
UsbDevice usbDevice = device.getDevice();
570-
if (!mUsbManager.hasPermission(usbDevice)) {
571-
HIDDeviceOpenPending(deviceID);
572-
try {
573-
mUsbManager.requestPermission(usbDevice, PendingIntent.getBroadcast(mContext, 0, new Intent(HIDDeviceManager.ACTION_USB_PERMISSION), 0));
574-
} catch (Exception e) {
575-
Log.v(TAG, "Couldn't request permission for USB device " + usbDevice);
576-
HIDDeviceOpenResult(deviceID, false);
577-
}
578-
return false;
579-
}
580-
break;
567+
UsbDevice usbDevice = device.getDevice();
568+
if (usbDevice != null && !mUsbManager.hasPermission(usbDevice)) {
569+
HIDDeviceOpenPending(deviceID);
570+
try {
571+
mUsbManager.requestPermission(usbDevice, PendingIntent.getBroadcast(mContext, 0, new Intent(HIDDeviceManager.ACTION_USB_PERMISSION), 0));
572+
} catch (Exception e) {
573+
Log.v(TAG, "Couldn't request permission for USB device " + usbDevice);
574+
HIDDeviceOpenResult(deviceID, false);
581575
}
576+
return false;
582577
}
583578

584579
try {
585-
Log.v(TAG, "openDevice deviceID=" + deviceID);
586-
HIDDevice device;
587-
device = getDevice(deviceID);
588-
if (device == null) {
589-
HIDDeviceDisconnected(deviceID);
590-
return false;
591-
}
592-
593580
return device.open();
594581
} catch (Exception e) {
595582
Log.e(TAG, "Got exception: " + Log.getStackTraceString(e));
@@ -599,7 +586,7 @@ public boolean openDevice(int deviceID) {
599586

600587
public int sendOutputReport(int deviceID, byte[] report) {
601588
try {
602-
Log.v(TAG, "sendOutputReport deviceID=" + deviceID + " length=" + report.length);
589+
//Log.v(TAG, "sendOutputReport deviceID=" + deviceID + " length=" + report.length);
603590
HIDDevice device;
604591
device = getDevice(deviceID);
605592
if (device == null) {
@@ -616,7 +603,7 @@ public int sendOutputReport(int deviceID, byte[] report) {
616603

617604
public int sendFeatureReport(int deviceID, byte[] report) {
618605
try {
619-
Log.v(TAG, "sendFeatureReport deviceID=" + deviceID + " length=" + report.length);
606+
//Log.v(TAG, "sendFeatureReport deviceID=" + deviceID + " length=" + report.length);
620607
HIDDevice device;
621608
device = getDevice(deviceID);
622609
if (device == null) {
@@ -633,7 +620,7 @@ public int sendFeatureReport(int deviceID, byte[] report) {
633620

634621
public boolean getFeatureReport(int deviceID, byte[] report) {
635622
try {
636-
Log.v(TAG, "getFeatureReport deviceID=" + deviceID);
623+
//Log.v(TAG, "getFeatureReport deviceID=" + deviceID);
637624
HIDDevice device;
638625
device = getDevice(deviceID);
639626
if (device == null) {
@@ -672,7 +659,7 @@ public void closeDevice(int deviceID) {
672659
private native void HIDDeviceRegisterCallback();
673660
private native void HIDDeviceReleaseCallback();
674661

675-
native void HIDDeviceConnected(int deviceID, String identifier, int vendorId, int productId, String serial_number, int release_number, String manufacturer_string, String product_string, int interface_number);
662+
native void HIDDeviceConnected(int deviceID, String identifier, int vendorId, int productId, String serial_number, int release_number, String manufacturer_string, String product_string, int interface_number, int interface_class, int interface_subclass, int interface_protocol);
676663
native void HIDDeviceOpenPending(int deviceID);
677664
native void HIDDeviceOpenResult(int deviceID, boolean opened);
678665
native void HIDDeviceDisconnected(int deviceID);

0 commit comments

Comments
 (0)