JoypadManager.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package com.xugame.app;
  2. import android.view.InputDevice;
  3. import android.view.MotionEvent;
  4. import com.xugame.BuildConfig;
  5. import com.xugame.gameconsole.util.DebugUtil;
  6. import java.util.ArrayList;
  7. import java.util.HashSet;
  8. public class JoypadManager {
  9. protected static JoypadHandler mJoystickHandler = null;
  10. public static void initialize() {
  11. if (mJoystickHandler == null) {
  12. mJoystickHandler = new JoypadHandler_API();
  13. }
  14. }
  15. public static void uninitialize() {
  16. mJoystickHandler.removeInputDevices();
  17. mJoystickHandler = null;
  18. }
  19. // Using for JNI
  20. public static void pollInputDevices() {
  21. mJoystickHandler.pollInputDevices();
  22. }
  23. // Check if a given device is considered a possible SDL joystick
  24. public static boolean isDeviceJoystick(int deviceId) {
  25. InputDevice device = InputDevice.getDevice(deviceId);
  26. // We cannot use InputDevice.isVirtual before API 16, so let's accept
  27. // only nonnegative device ids (VIRTUAL_KEYBOARD equals -1)
  28. if ((device == null) || (deviceId < 0)) {
  29. return false;
  30. }
  31. if (device.getName().equals("sunxi-gpadc"))
  32. return true;
  33. if (device.getName().equals("robot"))
  34. return true;
  35. if (device.getName().equals("P4"))
  36. return true;
  37. if (device.isVirtual()) {
  38. return false;
  39. }
  40. int sources = device.getSources();
  41. /* This is called for every button press, so let's not spam the logs */
  42. /*
  43. if ((sources & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
  44. Log.v(TAG, "Input device " + device.getName() + " has class joystick.");
  45. }
  46. if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) {
  47. Log.v(TAG, "Input device " + device.getName() + " is a dpad.");
  48. }
  49. if ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) {
  50. Log.v(TAG, "Input device " + device.getName() + " is a gamepad.");
  51. }
  52. */
  53. InputDevice.MotionRange range = device.getMotionRange(MotionEvent.AXIS_HAT_X);
  54. if (range == null)
  55. return false;
  56. range = device.getMotionRange(MotionEvent.AXIS_HAT_Y);
  57. if (range == null)
  58. return false;
  59. return ((sources & InputDevice.SOURCE_CLASS_JOYSTICK) != 0 ||
  60. ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) ||
  61. ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
  62. );
  63. }
  64. public static native void addJoystickNative(int deviceId, int deviceSource);
  65. public static native void removeJoystickNative(int deviceId);
  66. }
  67. class JoypadHandler {
  68. /**
  69. * Handles adding and removing of input devices.
  70. */
  71. public void pollInputDevices() {
  72. }
  73. public void removeInputDevices() {
  74. }
  75. }
  76. class JoypadHandler_API extends JoypadHandler {
  77. private final HashSet<Integer> mJoypadIds;
  78. public JoypadHandler_API() {
  79. mJoypadIds = new HashSet<Integer>();
  80. }
  81. @Override
  82. public void pollInputDevices() {
  83. int[] deviceIds = InputDevice.getDeviceIds();
  84. //20231205--如果指定1p首先增加
  85. if (BuildConfig.FIRST_DEVICE_ID >= 0) {
  86. int tempID = BuildConfig.FIRST_DEVICE_ID;
  87. for (int dID : deviceIds) {
  88. if (mJoypadIds != null && !mJoypadIds.contains(dID)) {
  89. if (tempID == dID) {
  90. InputDevice joypadDevice = InputDevice.getDevice(dID);
  91. mJoypadIds.add(dID);
  92. JoypadManager.addJoystickNative(
  93. dID,
  94. joypadDevice.getSources()
  95. );
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. for (int deviceId : deviceIds) {
  102. // DebugUtil.i("JOYPAD:",""+InputDevice.getDevice(deviceId).toString());
  103. if (!mJoypadIds.contains(deviceId)) {
  104. if (JoypadManager.isDeviceJoystick(deviceId)) {
  105. DebugUtil.i("JOYPAD has:", "" + InputDevice.getDevice(deviceId).toString());
  106. InputDevice joypadDevice = InputDevice.getDevice(deviceId);
  107. mJoypadIds.add(deviceId);
  108. JoypadManager.addJoystickNative(
  109. deviceId,
  110. joypadDevice.getSources()
  111. );
  112. }
  113. }
  114. }
  115. ArrayList<Integer> removedDevices = null;
  116. for (Integer joystickId : mJoypadIds) {
  117. int i;
  118. for (i = 0; i < deviceIds.length; i++) {
  119. if (joystickId == deviceIds[i])
  120. break;
  121. }
  122. if (i == deviceIds.length) {
  123. if (removedDevices == null) {
  124. removedDevices = new ArrayList<Integer>();
  125. }
  126. removedDevices.add(joystickId);
  127. }
  128. }
  129. if (removedDevices != null) {
  130. for (int deviceId : removedDevices) {
  131. JoypadManager.removeJoystickNative(deviceId);
  132. mJoypadIds.remove(deviceId);
  133. }
  134. }
  135. }
  136. @Override
  137. public void removeInputDevices() {
  138. for (int deviceId : mJoypadIds) {
  139. JoypadManager.removeJoystickNative(deviceId);
  140. }
  141. mJoypadIds.clear();
  142. }
  143. }