SDL  2.0
org.libsdl.app.HIDDeviceManager Class Reference
+ Collaboration diagram for org.libsdl.app.HIDDeviceManager:

Public Member Functions

Context getContext ()
 
int getDeviceIDForIdentifier (String identifier)
 
void chromebookConnectionHandler ()
 
boolean connectBluetoothDevice (BluetoothDevice bluetoothDevice)
 
void disconnectBluetoothDevice (BluetoothDevice bluetoothDevice)
 
boolean isSteamController (BluetoothDevice bluetoothDevice)
 
void setFrozen (boolean frozen)
 
boolean openDevice (int deviceID)
 
int sendOutputReport (int deviceID, byte[] report)
 
int sendFeatureReport (int deviceID, byte[] report)
 
boolean getFeatureReport (int deviceID, byte[] report)
 
void closeDevice (int deviceID)
 

Static Public Member Functions

static HIDDeviceManager acquire (Context context)
 
static void release (HIDDeviceManager manager)
 

Private Member Functions

 HIDDeviceManager (final Context context)
 
void initializeUSB ()
 
void shutdownUSB ()
 
boolean isHIDDeviceUSB (UsbDevice usbDevice)
 
boolean isHIDDeviceInterface (UsbDevice usbDevice, int interface_number)
 
boolean isXbox360Controller (UsbDevice usbDevice, UsbInterface usbInterface)
 
boolean isXboxOneController (UsbDevice usbDevice, UsbInterface usbInterface)
 
void handleUsbDeviceAttached (UsbDevice usbDevice)
 
void handleUsbDeviceDetached (UsbDevice usbDevice)
 
void handleUsbDevicePermission (UsbDevice usbDevice, boolean permission_granted)
 
void connectHIDDeviceUSB (UsbDevice usbDevice)
 
void initializeBluetooth ()
 
void shutdownBluetooth ()
 
void close ()
 
HIDDevice getDevice (int id)
 
native void HIDDeviceRegisterCallback ()
 
native void HIDDeviceReleaseCallback ()
 

Private Attributes

Context mContext
 
HashMap< Integer, HIDDevice > mDevicesById = new HashMap<Integer, HIDDevice>()
 
HashMap< UsbDevice, HIDDeviceUSB > mUSBDevices = new HashMap<UsbDevice, HIDDeviceUSB>()
 
HashMap< BluetoothDevice, HIDDeviceBLESteamController > mBluetoothDevices = new HashMap<BluetoothDevice, HIDDeviceBLESteamController>()
 
int mNextDeviceId = 0
 
SharedPreferences mSharedPreferences = null
 
boolean mIsChromebook = false
 
UsbManager mUsbManager
 
Handler mHandler
 
BluetoothManager mBluetoothManager
 
List< BluetoothDevice > mLastBluetoothDevices
 
final BroadcastReceiver mUsbBroadcast
 
final BroadcastReceiver mBluetoothBroadcast
 

Static Private Attributes

static final String TAG = "hidapi"
 
static final String ACTION_USB_PERMISSION = "org.libsdl.app.USB_PERMISSION"
 
static HIDDeviceManager sManager
 
static int sManagerRefCount = 0
 

Detailed Description

Definition at line 26 of file HIDDeviceManager.java.

Constructor & Destructor Documentation

◆ HIDDeviceManager()

org.libsdl.app.HIDDeviceManager.HIDDeviceManager ( final Context  context)
inlineprivate

Definition at line 104 of file HIDDeviceManager.java.

References context, e, org.libsdl.app.HIDDeviceManager.HIDDeviceRegisterCallback(), org.libsdl.app.HIDDeviceManager.initializeBluetooth(), org.libsdl.app.HIDDeviceManager.initializeUSB(), and org.libsdl.app.SDL.loadLibrary().

Referenced by org.libsdl.app.HIDDeviceManager.acquire().

104  {
105  mContext = context;
106 
107  // Make sure we have the HIDAPI library loaded with the native functions
108  try {
109  SDL.loadLibrary("hidapi");
110  } catch (Throwable e) {
111  Log.w(TAG, "Couldn't load hidapi: " + e.toString());
112 
113  AlertDialog.Builder builder = new AlertDialog.Builder(context);
114  builder.setCancelable(false);
115  builder.setTitle("SDL HIDAPI Error");
116  builder.setMessage("Please report the following error to the SDL maintainers: " + e.getMessage());
117  builder.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
118  @Override
119  public void onClick(DialogInterface dialog, int which) {
120  try {
121  // If our context is an activity, exit rather than crashing when we can't
122  // call our native functions.
123  Activity activity = (Activity)context;
124 
125  activity.finish();
126  }
127  catch (ClassCastException cce) {
128  // Context wasn't an activity, there's nothing we can do. Give up and return.
129  }
130  }
131  });
132  builder.show();
133 
134  return;
135  }
136 
138 
139  mSharedPreferences = mContext.getSharedPreferences("hidapi", Context.MODE_PRIVATE);
140  mIsChromebook = mContext.getPackageManager().hasSystemFeature("org.chromium.arc.device_management");
141 
142 // if (shouldClear) {
143 // SharedPreferences.Editor spedit = mSharedPreferences.edit();
144 // spedit.clear();
145 // spedit.commit();
146 // }
147 // else
148  {
149  mNextDeviceId = mSharedPreferences.getInt("next_device_id", 0);
150  }
151 
152  initializeUSB();
154  }
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
static screen_context_t context
Definition: video.c:25
native void HIDDeviceRegisterCallback()

Member Function Documentation

◆ acquire()

static HIDDeviceManager org.libsdl.app.HIDDeviceManager.acquire ( Context  context)
inlinestatic

◆ chromebookConnectionHandler()

void org.libsdl.app.HIDDeviceManager.chromebookConnectionHandler ( )
inline

Definition at line 437 of file HIDDeviceManager.java.

References org.libsdl.app.HIDDeviceManager.chromebookConnectionHandler(), org.libsdl.app.HIDDeviceManager.connectBluetoothDevice(), and org.libsdl.app.HIDDeviceManager.disconnectBluetoothDevice().

Referenced by org.libsdl.app.HIDDeviceManager.chromebookConnectionHandler().

437  {
438  if (!mIsChromebook) {
439  return;
440  }
441 
442  ArrayList<BluetoothDevice> disconnected = new ArrayList<>();
443  ArrayList<BluetoothDevice> connected = new ArrayList<>();
444 
445  List<BluetoothDevice> currentConnected = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
446 
447  for (BluetoothDevice bluetoothDevice : currentConnected) {
448  if (!mLastBluetoothDevices.contains(bluetoothDevice)) {
449  connected.add(bluetoothDevice);
450  }
451  }
452  for (BluetoothDevice bluetoothDevice : mLastBluetoothDevices) {
453  if (!currentConnected.contains(bluetoothDevice)) {
454  disconnected.add(bluetoothDevice);
455  }
456  }
457 
458  mLastBluetoothDevices = currentConnected;
459 
460  for (BluetoothDevice bluetoothDevice : disconnected) {
461  disconnectBluetoothDevice(bluetoothDevice);
462  }
463  for (BluetoothDevice bluetoothDevice : connected) {
464  connectBluetoothDevice(bluetoothDevice);
465  }
466 
467  final HIDDeviceManager finalThis = this;
468  mHandler.postDelayed(new Runnable() {
469  @Override
470  public void run() {
471  finalThis.chromebookConnectionHandler();
472  }
473  }, 10000);
474  }
boolean connectBluetoothDevice(BluetoothDevice bluetoothDevice)
List< BluetoothDevice > mLastBluetoothDevices
void disconnectBluetoothDevice(BluetoothDevice bluetoothDevice)
HIDDeviceManager(final Context context)

◆ close()

void org.libsdl.app.HIDDeviceManager.close ( )
inlineprivate

Definition at line 525 of file HIDDeviceManager.java.

References device, org.libsdl.app.HIDDeviceManager.HIDDeviceReleaseCallback(), org.libsdl.app.HIDDeviceManager.shutdownBluetooth(), and org.libsdl.app.HIDDeviceManager.shutdownUSB().

Referenced by org.libsdl.app.HIDDeviceManager.release().

525  {
526  shutdownUSB();
528  synchronized (this) {
529  for (HIDDevice device : mDevicesById.values()) {
530  device.shutdown();
531  }
532  mDevicesById.clear();
533  mBluetoothDevices.clear();
535  }
536  }
native void HIDDeviceReleaseCallback()
HashMap< Integer, HIDDevice > mDevicesById
static SDL_AudioDeviceID device
Definition: loopwave.c:37
HashMap< BluetoothDevice, HIDDeviceBLESteamController > mBluetoothDevices

◆ closeDevice()

void org.libsdl.app.HIDDeviceManager.closeDevice ( int  deviceID)
inline

Definition at line 651 of file HIDDeviceManager.java.

References device, e, org.libsdl.app.HIDDeviceManager.getDevice(), org.libsdl.app.HIDDeviceManager.HIDDeviceRegisterCallback(), and org.libsdl.app.HIDDeviceManager.HIDDeviceReleaseCallback().

651  {
652  try {
653  Log.v(TAG, "closeDevice deviceID=" + deviceID);
654  HIDDevice device;
655  device = getDevice(deviceID);
656  if (device == null) {
657  HIDDeviceDisconnected(deviceID);
658  return;
659  }
660 
661  device.close();
662  } catch (Exception e) {
663  Log.e(TAG, "Got exception: " + Log.getStackTraceString(e));
664  }
665  }
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
static SDL_AudioDeviceID device
Definition: loopwave.c:37
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceDisconnected(JNIEnv *env, jobject thiz, int nDeviceID)
Definition: hid.cpp:887

◆ connectBluetoothDevice()

boolean org.libsdl.app.HIDDeviceManager.connectBluetoothDevice ( BluetoothDevice  bluetoothDevice)
inline

Definition at line 476 of file HIDDeviceManager.java.

References device.

Referenced by org.libsdl.app.HIDDeviceManager.chromebookConnectionHandler(), and org.libsdl.app.HIDDeviceManager.initializeBluetooth().

476  {
477  Log.v(TAG, "connectBluetoothDevice device=" + bluetoothDevice);
478  synchronized (this) {
479  if (mBluetoothDevices.containsKey(bluetoothDevice)) {
480  Log.v(TAG, "Steam controller with address " + bluetoothDevice + " already exists, attempting reconnect");
481 
482  HIDDeviceBLESteamController device = mBluetoothDevices.get(bluetoothDevice);
483  device.reconnect();
484 
485  return false;
486  }
487  HIDDeviceBLESteamController device = new HIDDeviceBLESteamController(this, bluetoothDevice);
488  int id = device.getId();
489  mBluetoothDevices.put(bluetoothDevice, device);
490  mDevicesById.put(id, device);
491 
492  // The Steam Controller will mark itself connected once initialization is complete
493  }
494  return true;
495  }
HashMap< Integer, HIDDevice > mDevicesById
static SDL_AudioDeviceID device
Definition: loopwave.c:37
HashMap< BluetoothDevice, HIDDeviceBLESteamController > mBluetoothDevices

◆ connectHIDDeviceUSB()

void org.libsdl.app.HIDDeviceManager.connectHIDDeviceUSB ( UsbDevice  usbDevice)
inlineprivate

Definition at line 360 of file HIDDeviceManager.java.

References device, and org.libsdl.app.HIDDeviceManager.isHIDDeviceInterface().

Referenced by org.libsdl.app.HIDDeviceManager.handleUsbDeviceAttached().

360  {
361  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);
365  int id = device.getId();
366  mUSBDevices.put(usbDevice, device);
367  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;
370  }
371  }
372  }
373  }
int interface_number
Definition: hidapi.h:79
HashMap< Integer, HIDDevice > mDevicesById
static SDL_AudioDeviceID device
Definition: loopwave.c:37
HashMap< UsbDevice, HIDDeviceUSB > mUSBDevices
boolean isHIDDeviceInterface(UsbDevice usbDevice, int interface_number)
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceConnected(JNIEnv *env, jobject thiz, int nDeviceID, jstring sIdentifier, int nVendorId, int nProductId, jstring sSerialNumber, int nReleaseNumber, jstring sManufacturer, jstring sProduct, int nInterface)
Definition: hid.cpp:831

◆ disconnectBluetoothDevice()

void org.libsdl.app.HIDDeviceManager.disconnectBluetoothDevice ( BluetoothDevice  bluetoothDevice)
inline

Definition at line 497 of file HIDDeviceManager.java.

References device.

Referenced by org.libsdl.app.HIDDeviceManager.chromebookConnectionHandler().

497  {
498  synchronized (this) {
499  HIDDeviceBLESteamController device = mBluetoothDevices.get(bluetoothDevice);
500  if (device == null)
501  return;
502 
503  int id = device.getId();
504  mBluetoothDevices.remove(bluetoothDevice);
505  mDevicesById.remove(id);
506  device.shutdown();
508  }
509  }
HashMap< Integer, HIDDevice > mDevicesById
static SDL_AudioDeviceID device
Definition: loopwave.c:37
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceDisconnected(JNIEnv *env, jobject thiz, int nDeviceID)
Definition: hid.cpp:887
HashMap< BluetoothDevice, HIDDeviceBLESteamController > mBluetoothDevices

◆ getContext()

Context org.libsdl.app.HIDDeviceManager.getContext ( )
inline

Definition at line 156 of file HIDDeviceManager.java.

References org.libsdl.app.HIDDeviceManager.mContext.

156  {
157  return mContext;
158  }

◆ getDevice()

HIDDevice org.libsdl.app.HIDDeviceManager.getDevice ( int  id)
inlineprivate

Definition at line 550 of file HIDDeviceManager.java.

Referenced by org.libsdl.app.HIDDeviceManager.closeDevice(), org.libsdl.app.HIDDeviceManager.getFeatureReport(), org.libsdl.app.HIDDeviceManager.openDevice(), org.libsdl.app.HIDDeviceManager.sendFeatureReport(), and org.libsdl.app.HIDDeviceManager.sendOutputReport().

550  {
551  synchronized (this) {
552  HIDDevice result = mDevicesById.get(id);
553  if (result == null) {
554  Log.v(TAG, "No device for id: " + id);
555  Log.v(TAG, "Available devices: " + mDevicesById.keySet());
556  }
557  return result;
558  }
559  }
HashMap< Integer, HIDDevice > mDevicesById
GLuint64EXT * result

◆ getDeviceIDForIdentifier()

int org.libsdl.app.HIDDeviceManager.getDeviceIDForIdentifier ( String  identifier)
inline

Definition at line 160 of file HIDDeviceManager.java.

160  {
161  SharedPreferences.Editor spedit = mSharedPreferences.edit();
162 
163  int result = mSharedPreferences.getInt(identifier, 0);
164  if (result == 0) {
165  result = mNextDeviceId++;
166  spedit.putInt("next_device_id", mNextDeviceId);
167  }
168 
169  spedit.putInt(identifier, result);
170  spedit.commit();
171  return result;
172  }
GLuint64EXT * result

◆ getFeatureReport()

boolean org.libsdl.app.HIDDeviceManager.getFeatureReport ( int  deviceID,
byte []  report 
)
inline

Definition at line 634 of file HIDDeviceManager.java.

References device, e, and org.libsdl.app.HIDDeviceManager.getDevice().

634  {
635  try {
636  Log.v(TAG, "getFeatureReport deviceID=" + deviceID);
637  HIDDevice device;
638  device = getDevice(deviceID);
639  if (device == null) {
640  HIDDeviceDisconnected(deviceID);
641  return false;
642  }
643 
644  return device.getFeatureReport(report);
645  } catch (Exception e) {
646  Log.e(TAG, "Got exception: " + Log.getStackTraceString(e));
647  }
648  return false;
649  }
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
static SDL_AudioDeviceID device
Definition: loopwave.c:37
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceDisconnected(JNIEnv *env, jobject thiz, int nDeviceID)
Definition: hid.cpp:887

◆ handleUsbDeviceAttached()

void org.libsdl.app.HIDDeviceManager.handleUsbDeviceAttached ( UsbDevice  usbDevice)
inlineprivate

Definition at line 330 of file HIDDeviceManager.java.

References org.libsdl.app.HIDDeviceManager.connectHIDDeviceUSB(), and org.libsdl.app.HIDDeviceManager.isHIDDeviceUSB().

Referenced by org.libsdl.app.HIDDeviceManager.initializeUSB().

330  {
331  if (isHIDDeviceUSB(usbDevice)) {
332  connectHIDDeviceUSB(usbDevice);
333  }
334  }
void connectHIDDeviceUSB(UsbDevice usbDevice)
boolean isHIDDeviceUSB(UsbDevice usbDevice)

◆ handleUsbDeviceDetached()

void org.libsdl.app.HIDDeviceManager.handleUsbDeviceDetached ( UsbDevice  usbDevice)
inlineprivate

Definition at line 336 of file HIDDeviceManager.java.

References device.

336  {
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();
346  }
HashMap< Integer, HIDDevice > mDevicesById
static SDL_AudioDeviceID device
Definition: loopwave.c:37
HashMap< UsbDevice, HIDDeviceUSB > mUSBDevices
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceDisconnected(JNIEnv *env, jobject thiz, int nDeviceID)
Definition: hid.cpp:887

◆ handleUsbDevicePermission()

void org.libsdl.app.HIDDeviceManager.handleUsbDevicePermission ( UsbDevice  usbDevice,
boolean  permission_granted 
)
inlineprivate

Definition at line 348 of file HIDDeviceManager.java.

References device.

348  {
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();
356  }
357  HIDDeviceOpenResult(device.getId(), opened);
358  }
static SDL_AudioDeviceID device
Definition: loopwave.c:37
HashMap< UsbDevice, HIDDeviceUSB > mUSBDevices
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceOpenResult(JNIEnv *env, jobject thiz, int nDeviceID, bool bOpened)
Definition: hid.cpp:876

◆ HIDDeviceRegisterCallback()

native void org.libsdl.app.HIDDeviceManager.HIDDeviceRegisterCallback ( )
private

◆ HIDDeviceReleaseCallback()

native void org.libsdl.app.HIDDeviceManager.HIDDeviceReleaseCallback ( )
private

◆ initializeBluetooth()

void org.libsdl.app.HIDDeviceManager.initializeBluetooth ( )
inlineprivate

Definition at line 375 of file HIDDeviceManager.java.

References org.libsdl.app.HIDDeviceManager.connectBluetoothDevice(), device, and org.libsdl.app.HIDDeviceManager.isSteamController().

Referenced by org.libsdl.app.HIDDeviceManager.HIDDeviceManager().

375  {
376  Log.d(TAG, "Initializing Bluetooth");
377 
378  if (mContext.getPackageManager().checkPermission(android.Manifest.permission.BLUETOOTH, mContext.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
379  Log.d(TAG, "Couldn't initialize Bluetooth, missing android.permission.BLUETOOTH");
380  return;
381  }
382 
383  // Find bonded bluetooth controllers and create SteamControllers for them
384  mBluetoothManager = (BluetoothManager)mContext.getSystemService(Context.BLUETOOTH_SERVICE);
385  if (mBluetoothManager == null) {
386  // This device doesn't support Bluetooth.
387  return;
388  }
389 
390  BluetoothAdapter btAdapter = mBluetoothManager.getAdapter();
391  if (btAdapter == null) {
392  // This device has Bluetooth support in the codebase, but has no available adapters.
393  return;
394  }
395 
396  // Get our bonded devices.
397  for (BluetoothDevice device : btAdapter.getBondedDevices()) {
398 
399  Log.d(TAG, "Bluetooth device available: " + device);
400  if (isSteamController(device)) {
402  }
403 
404  }
405 
406  // NOTE: These don't work on Chromebooks, to my undying dismay.
407  IntentFilter filter = new IntentFilter();
408  filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
409  filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
410  mContext.registerReceiver(mBluetoothBroadcast, filter);
411 
412  if (mIsChromebook) {
413  mHandler = new Handler(Looper.getMainLooper());
414  mLastBluetoothDevices = new ArrayList<>();
415 
416  // final HIDDeviceManager finalThis = this;
417  // mHandler.postDelayed(new Runnable() {
418  // @Override
419  // public void run() {
420  // finalThis.chromebookConnectionHandler();
421  // }
422  // }, 5000);
423  }
424  }
boolean connectBluetoothDevice(BluetoothDevice bluetoothDevice)
final BroadcastReceiver mBluetoothBroadcast
List< BluetoothDevice > mLastBluetoothDevices
static SDL_AudioDeviceID device
Definition: loopwave.c:37
boolean isSteamController(BluetoothDevice bluetoothDevice)
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter

◆ initializeUSB()

void org.libsdl.app.HIDDeviceManager.initializeUSB ( )
inlineprivate

Definition at line 174 of file HIDDeviceManager.java.

References org.libsdl.app.HIDDeviceManager.ACTION_USB_PERMISSION, org.libsdl.app.HIDDeviceManager.handleUsbDeviceAttached(), and org.libsdl.app.HIDDeviceManager.mUsbManager.

Referenced by org.libsdl.app.HIDDeviceManager.HIDDeviceManager().

174  {
175  mUsbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
176 
177  /*
178  // Logging
179  for (UsbDevice device : mUsbManager.getDeviceList().values()) {
180  Log.i(TAG,"Path: " + device.getDeviceName());
181  Log.i(TAG,"Manufacturer: " + device.getManufacturerName());
182  Log.i(TAG,"Product: " + device.getProductName());
183  Log.i(TAG,"ID: " + device.getDeviceId());
184  Log.i(TAG,"Class: " + device.getDeviceClass());
185  Log.i(TAG,"Protocol: " + device.getDeviceProtocol());
186  Log.i(TAG,"Vendor ID " + device.getVendorId());
187  Log.i(TAG,"Product ID: " + device.getProductId());
188  Log.i(TAG,"Interface count: " + device.getInterfaceCount());
189  Log.i(TAG,"---------------------------------------");
190 
191  // Get interface details
192  for (int index = 0; index < device.getInterfaceCount(); index++) {
193  UsbInterface mUsbInterface = device.getInterface(index);
194  Log.i(TAG," ***** *****");
195  Log.i(TAG," Interface index: " + index);
196  Log.i(TAG," Interface ID: " + mUsbInterface.getId());
197  Log.i(TAG," Interface class: " + mUsbInterface.getInterfaceClass());
198  Log.i(TAG," Interface subclass: " + mUsbInterface.getInterfaceSubclass());
199  Log.i(TAG," Interface protocol: " + mUsbInterface.getInterfaceProtocol());
200  Log.i(TAG," Endpoint count: " + mUsbInterface.getEndpointCount());
201 
202  // Get endpoint details
203  for (int epi = 0; epi < mUsbInterface.getEndpointCount(); epi++)
204  {
205  UsbEndpoint mEndpoint = mUsbInterface.getEndpoint(epi);
206  Log.i(TAG," ++++ ++++ ++++");
207  Log.i(TAG," Endpoint index: " + epi);
208  Log.i(TAG," Attributes: " + mEndpoint.getAttributes());
209  Log.i(TAG," Direction: " + mEndpoint.getDirection());
210  Log.i(TAG," Number: " + mEndpoint.getEndpointNumber());
211  Log.i(TAG," Interval: " + mEndpoint.getInterval());
212  Log.i(TAG," Packet size: " + mEndpoint.getMaxPacketSize());
213  Log.i(TAG," Type: " + mEndpoint.getType());
214  }
215  }
216  }
217  Log.i(TAG," No more devices connected.");
218  */
219 
220  // Register for USB broadcasts and permission completions
221  IntentFilter filter = new IntentFilter();
222  filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
223  filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
224  filter.addAction(HIDDeviceManager.ACTION_USB_PERMISSION);
225  mContext.registerReceiver(mUsbBroadcast, filter);
226 
227  for (UsbDevice usbDevice : mUsbManager.getDeviceList().values()) {
228  handleUsbDeviceAttached(usbDevice);
229  }
230  }
void handleUsbDeviceAttached(UsbDevice usbDevice)
final BroadcastReceiver mUsbBroadcast
HIDDeviceManager(final Context context)
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter

◆ isHIDDeviceInterface()

boolean org.libsdl.app.HIDDeviceManager.isHIDDeviceInterface ( UsbDevice  usbDevice,
int  interface_number 
)
inlineprivate

Definition at line 253 of file HIDDeviceManager.java.

References org.libsdl.app.HIDDeviceManager.isXbox360Controller(), and org.libsdl.app.HIDDeviceManager.isXboxOneController().

Referenced by org.libsdl.app.HIDDeviceManager.connectHIDDeviceUSB(), and org.libsdl.app.HIDDeviceManager.isHIDDeviceUSB().

253  {
254  UsbInterface usbInterface = usbDevice.getInterface(interface_number);
255  if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_HID) {
256  return true;
257  }
258  if (interface_number == 0) {
259  if (isXbox360Controller(usbDevice, usbInterface) || isXboxOneController(usbDevice, usbInterface)) {
260  return true;
261  }
262  }
263  return false;
264  }
int interface_number
Definition: hidapi.h:79
boolean isXboxOneController(UsbDevice usbDevice, UsbInterface usbInterface)
boolean isXbox360Controller(UsbDevice usbDevice, UsbInterface usbInterface)

◆ isHIDDeviceUSB()

boolean org.libsdl.app.HIDDeviceManager.isHIDDeviceUSB ( UsbDevice  usbDevice)
inlineprivate

Definition at line 244 of file HIDDeviceManager.java.

References org.libsdl.app.HIDDeviceManager.isHIDDeviceInterface().

Referenced by org.libsdl.app.HIDDeviceManager.handleUsbDeviceAttached().

244  {
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  }
int interface_number
Definition: hidapi.h:79
boolean isHIDDeviceInterface(UsbDevice usbDevice, int interface_number)

◆ isSteamController()

boolean org.libsdl.app.HIDDeviceManager.isSteamController ( BluetoothDevice  bluetoothDevice)
inline

Definition at line 511 of file HIDDeviceManager.java.

Referenced by org.libsdl.app.HIDDeviceManager.initializeBluetooth().

511  {
512  // Sanity check. If you pass in a null device, by definition it is never a Steam Controller.
513  if (bluetoothDevice == null) {
514  return false;
515  }
516 
517  // If the device has no local name, we really don't want to try an equality check against it.
518  if (bluetoothDevice.getName() == null) {
519  return false;
520  }
521 
522  return bluetoothDevice.getName().equals("SteamController") && ((bluetoothDevice.getType() & BluetoothDevice.DEVICE_TYPE_LE) != 0);
523  }

◆ isXbox360Controller()

boolean org.libsdl.app.HIDDeviceManager.isXbox360Controller ( UsbDevice  usbDevice,
UsbInterface  usbInterface 
)
inlineprivate

Definition at line 266 of file HIDDeviceManager.java.

Referenced by org.libsdl.app.HIDDeviceManager.isHIDDeviceInterface().

266  {
267  final int XB360_IFACE_SUBCLASS = 93;
268  final int XB360_IFACE_PROTOCOL = 1; // Wired only
269  final int[] SUPPORTED_VENDORS = {
270  0x0079, // GPD Win 2
271  0x044f, // Thrustmaster
272  0x045e, // Microsoft
273  0x046d, // Logitech
274  0x056e, // Elecom
275  0x06a3, // Saitek
276  0x0738, // Mad Catz
277  0x07ff, // Mad Catz
278  0x0e6f, // Unknown
279  0x0f0d, // Hori
280  0x11c9, // Nacon
281  0x12ab, // Unknown
282  0x1430, // RedOctane
283  0x146b, // BigBen
284  0x1532, // Razer Sabertooth
285  0x15e4, // Numark
286  0x162e, // Joytech
287  0x1689, // Razer Onza
288  0x1bad, // Harmonix
289  0x24c6, // PowerA
290  };
291 
292  if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC &&
293  usbInterface.getInterfaceSubclass() == XB360_IFACE_SUBCLASS &&
294  usbInterface.getInterfaceProtocol() == XB360_IFACE_PROTOCOL) {
295  int vendor_id = usbDevice.getVendorId();
296  for (int supportedVid : SUPPORTED_VENDORS) {
297  if (vendor_id == supportedVid) {
298  return true;
299  }
300  }
301  }
302  return false;
303  }
unsigned short vendor_id
Definition: hidapi.h:57

◆ isXboxOneController()

boolean org.libsdl.app.HIDDeviceManager.isXboxOneController ( UsbDevice  usbDevice,
UsbInterface  usbInterface 
)
inlineprivate

Definition at line 305 of file HIDDeviceManager.java.

Referenced by org.libsdl.app.HIDDeviceManager.isHIDDeviceInterface().

305  {
306  final int XB1_IFACE_SUBCLASS = 71;
307  final int XB1_IFACE_PROTOCOL = 208;
308  final int[] SUPPORTED_VENDORS = {
309  0x045e, // Microsoft
310  0x0738, // Mad Catz
311  0x0e6f, // Unknown
312  0x0f0d, // Hori
313  0x1532, // Razer Wildcat
314  0x24c6, // PowerA
315  };
316 
317  if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC &&
318  usbInterface.getInterfaceSubclass() == XB1_IFACE_SUBCLASS &&
319  usbInterface.getInterfaceProtocol() == XB1_IFACE_PROTOCOL) {
320  int vendor_id = usbDevice.getVendorId();
321  for (int supportedVid : SUPPORTED_VENDORS) {
322  if (vendor_id == supportedVid) {
323  return true;
324  }
325  }
326  }
327  return false;
328  }
unsigned short vendor_id
Definition: hidapi.h:57

◆ openDevice()

boolean org.libsdl.app.HIDDeviceManager.openDevice ( int  deviceID)
inline

Definition at line 565 of file HIDDeviceManager.java.

References org.libsdl.app.HIDDeviceManager.ACTION_USB_PERMISSION, device, e, and org.libsdl.app.HIDDeviceManager.getDevice().

565  {
566  // 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;
581  }
582  }
583 
584  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 
593  return device.open();
594  } catch (Exception e) {
595  Log.e(TAG, "Got exception: " + Log.getStackTraceString(e));
596  }
597  return false;
598  }
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceOpenPending(JNIEnv *env, jobject thiz, int nDeviceID)
Definition: hid.cpp:865
static SDL_AudioDeviceID device
Definition: loopwave.c:37
HashMap< UsbDevice, HIDDeviceUSB > mUSBDevices
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceDisconnected(JNIEnv *env, jobject thiz, int nDeviceID)
Definition: hid.cpp:887
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceOpenResult(JNIEnv *env, jobject thiz, int nDeviceID, bool bOpened)
Definition: hid.cpp:876
HIDDeviceManager(final Context context)

◆ release()

static void org.libsdl.app.HIDDeviceManager.release ( HIDDeviceManager  manager)
inlinestatic

Definition at line 41 of file HIDDeviceManager.java.

References org.libsdl.app.HIDDeviceManager.close(), and org.libsdl.app.HIDDeviceManager.sManagerRefCount.

Referenced by org.libsdl.app.SDLActivity.onDestroy().

41  {
42  if (manager == sManager) {
44  if (sManagerRefCount == 0) {
45  sManager.close();
46  sManager = null;
47  }
48  }
49  }
static HIDDeviceManager sManager

◆ sendFeatureReport()

int org.libsdl.app.HIDDeviceManager.sendFeatureReport ( int  deviceID,
byte []  report 
)
inline

Definition at line 617 of file HIDDeviceManager.java.

References device, e, and org.libsdl.app.HIDDeviceManager.getDevice().

617  {
618  try {
619  Log.v(TAG, "sendFeatureReport deviceID=" + deviceID + " length=" + report.length);
620  HIDDevice device;
621  device = getDevice(deviceID);
622  if (device == null) {
623  HIDDeviceDisconnected(deviceID);
624  return -1;
625  }
626 
627  return device.sendFeatureReport(report);
628  } catch (Exception e) {
629  Log.e(TAG, "Got exception: " + Log.getStackTraceString(e));
630  }
631  return -1;
632  }
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
static SDL_AudioDeviceID device
Definition: loopwave.c:37
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceDisconnected(JNIEnv *env, jobject thiz, int nDeviceID)
Definition: hid.cpp:887

◆ sendOutputReport()

int org.libsdl.app.HIDDeviceManager.sendOutputReport ( int  deviceID,
byte []  report 
)
inline

Definition at line 600 of file HIDDeviceManager.java.

References device, e, and org.libsdl.app.HIDDeviceManager.getDevice().

600  {
601  try {
602  Log.v(TAG, "sendOutputReport deviceID=" + deviceID + " length=" + report.length);
603  HIDDevice device;
604  device = getDevice(deviceID);
605  if (device == null) {
606  HIDDeviceDisconnected(deviceID);
607  return -1;
608  }
609 
610  return device.sendOutputReport(report);
611  } catch (Exception e) {
612  Log.e(TAG, "Got exception: " + Log.getStackTraceString(e));
613  }
614  return -1;
615  }
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
static SDL_AudioDeviceID device
Definition: loopwave.c:37
JNIEXPORT void JNICALL HID_DEVICE_MANAGER_JAVA_INTERFACE() HIDDeviceDisconnected(JNIEnv *env, jobject thiz, int nDeviceID)
Definition: hid.cpp:887

◆ setFrozen()

void org.libsdl.app.HIDDeviceManager.setFrozen ( boolean  frozen)
inline

Definition at line 538 of file HIDDeviceManager.java.

References device.

Referenced by org.libsdl.app.SDLActivity.onPause(), and org.libsdl.app.SDLActivity.onResume().

538  {
539  synchronized (this) {
540  for (HIDDevice device : mDevicesById.values()) {
541  device.setFrozen(frozen);
542  }
543  }
544  }
HashMap< Integer, HIDDevice > mDevicesById
static SDL_AudioDeviceID device
Definition: loopwave.c:37

◆ shutdownBluetooth()

void org.libsdl.app.HIDDeviceManager.shutdownBluetooth ( )
inlineprivate

Definition at line 426 of file HIDDeviceManager.java.

References e.

Referenced by org.libsdl.app.HIDDeviceManager.close().

426  {
427  try {
428  mContext.unregisterReceiver(mBluetoothBroadcast);
429  } catch (Exception e) {
430  // We may not have registered, that's okay
431  }
432  }
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
final BroadcastReceiver mBluetoothBroadcast

◆ shutdownUSB()

void org.libsdl.app.HIDDeviceManager.shutdownUSB ( )
inlineprivate

Definition at line 236 of file HIDDeviceManager.java.

References e.

Referenced by org.libsdl.app.HIDDeviceManager.close().

236  {
237  try {
238  mContext.unregisterReceiver(mUsbBroadcast);
239  } catch (Exception e) {
240  // We may not have registered, that's okay
241  }
242  }
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
final BroadcastReceiver mUsbBroadcast

Field Documentation

◆ ACTION_USB_PERMISSION

final String org.libsdl.app.HIDDeviceManager.ACTION_USB_PERMISSION = "org.libsdl.app.USB_PERMISSION"
staticprivate

◆ mBluetoothBroadcast

final BroadcastReceiver org.libsdl.app.HIDDeviceManager.mBluetoothBroadcast
private
Initial value:
= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "Bluetooth device connected: " + device);
if (isSteamController(device)) {
}
}
if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "Bluetooth device disconnected: " + device);
}
}
}

Definition at line 80 of file HIDDeviceManager.java.

◆ mBluetoothDevices

HashMap<BluetoothDevice, HIDDeviceBLESteamController> org.libsdl.app.HIDDeviceManager.mBluetoothDevices = new HashMap<BluetoothDevice, HIDDeviceBLESteamController>()
private

Definition at line 54 of file HIDDeviceManager.java.

◆ mBluetoothManager

BluetoothManager org.libsdl.app.HIDDeviceManager.mBluetoothManager
private

Definition at line 60 of file HIDDeviceManager.java.

◆ mContext

Context org.libsdl.app.HIDDeviceManager.mContext
private

Definition at line 51 of file HIDDeviceManager.java.

Referenced by org.libsdl.app.HIDDeviceManager.getContext().

◆ mDevicesById

HashMap<Integer, HIDDevice> org.libsdl.app.HIDDeviceManager.mDevicesById = new HashMap<Integer, HIDDevice>()
private

Definition at line 52 of file HIDDeviceManager.java.

◆ mHandler

Handler org.libsdl.app.HIDDeviceManager.mHandler
private

Definition at line 59 of file HIDDeviceManager.java.

◆ mIsChromebook

boolean org.libsdl.app.HIDDeviceManager.mIsChromebook = false
private

Definition at line 57 of file HIDDeviceManager.java.

◆ mLastBluetoothDevices

List<BluetoothDevice> org.libsdl.app.HIDDeviceManager.mLastBluetoothDevices
private

Definition at line 61 of file HIDDeviceManager.java.

◆ mNextDeviceId

int org.libsdl.app.HIDDeviceManager.mNextDeviceId = 0
private

Definition at line 55 of file HIDDeviceManager.java.

◆ mSharedPreferences

SharedPreferences org.libsdl.app.HIDDeviceManager.mSharedPreferences = null
private

Definition at line 56 of file HIDDeviceManager.java.

◆ mUsbBroadcast

final BroadcastReceiver org.libsdl.app.HIDDeviceManager.mUsbBroadcast
private
Initial value:
= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
} else if (action.equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
} else if (action.equals(HIDDeviceManager.ACTION_USB_PERMISSION)) {
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
handleUsbDevicePermission(usbDevice, intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false));
}
}
}

Definition at line 63 of file HIDDeviceManager.java.

◆ mUSBDevices

HashMap<UsbDevice, HIDDeviceUSB> org.libsdl.app.HIDDeviceManager.mUSBDevices = new HashMap<UsbDevice, HIDDeviceUSB>()
private

Definition at line 53 of file HIDDeviceManager.java.

◆ mUsbManager

UsbManager org.libsdl.app.HIDDeviceManager.mUsbManager
private

Definition at line 58 of file HIDDeviceManager.java.

Referenced by org.libsdl.app.HIDDeviceManager.initializeUSB().

◆ sManager

HIDDeviceManager org.libsdl.app.HIDDeviceManager.sManager
staticprivate

Definition at line 30 of file HIDDeviceManager.java.

Referenced by org.libsdl.app.HIDDeviceManager.acquire().

◆ sManagerRefCount

int org.libsdl.app.HIDDeviceManager.sManagerRefCount = 0
staticprivate

◆ TAG

final String org.libsdl.app.HIDDeviceManager.TAG = "hidapi"
staticprivate

Definition at line 27 of file HIDDeviceManager.java.


The documentation for this class was generated from the following file: