Fixed bug 2361 - [Android] Joysticks do not have unique IDs David Brady When I attempted to make a mapping file for Android gamepads, I quickly discovered that most of the ones that I have here show up as the same device (Broadcom Bluetooth HID), meaning that it was impossible to make mappings on Android, since every device looked the same. This patch will check for the existence of the getDescriptor function added in Jelly Bean, and use it if it's there. The Android Dashboard says that the majority of Android phones should support this function, and doing it this way will not force us to bump up our API version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java
index aa232dc..b1bbf1e 100644
--- a/android-project/src/org/libsdl/app/SDLActivity.java
+++ b/android-project/src/org/libsdl/app/SDLActivity.java
@@ -182,12 +182,15 @@ public class SDLActivity extends Activity {
// Set up the surface
mSurface = new SDLSurface(getApplication());
- if(Build.VERSION.SDK_INT >= 12) {
+
+ if(Build.VERSION.SDK_INT >= 16) {
+ mJoystickHandler = new SDLJoystickHandler_API16();
+ } else if(Build.VERSION.SDK_INT >= 12) {
mJoystickHandler = new SDLJoystickHandler_API12();
- }
- else {
+ } else {
mJoystickHandler = new SDLJoystickHandler();
}
+
mHapticHandler = new SDLHapticHandler();
if (Build.VERSION.SDK_INT >= 11) {
@@ -511,7 +514,7 @@ public class SDLActivity extends Activity {
public static native void onNativeClipboardChanged();
public static native void onNativeSurfaceChanged();
public static native void onNativeSurfaceDestroyed();
- public static native int nativeAddJoystick(int device_id, String name,
+ public static native int nativeAddJoystick(int device_id, String name, String desc,
int is_accelerometer, int nbuttons,
int naxes, int nhats, int nballs);
public static native int nativeRemoveJoystick(int device_id);
@@ -1737,6 +1740,7 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
static class SDLJoystick {
public int device_id;
public String name;
+ public String desc;
public ArrayList<InputDevice.MotionRange> axes;
public ArrayList<InputDevice.MotionRange> hats;
}
@@ -1770,6 +1774,7 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
if (SDLActivity.isDeviceSDLJoystick(deviceIds[i])) {
joystick.device_id = deviceIds[i];
joystick.name = joystickDevice.getName();
+ joystick.desc = getJoystickDescriptor(joystickDevice);
joystick.axes = new ArrayList<InputDevice.MotionRange>();
joystick.hats = new ArrayList<InputDevice.MotionRange>();
@@ -1788,7 +1793,7 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
}
mJoysticks.add(joystick);
- SDLActivity.nativeAddJoystick(joystick.device_id, joystick.name, 0, -1,
+ SDLActivity.nativeAddJoystick(joystick.device_id, joystick.name, joystick.desc, 0, -1,
joystick.axes.size(), joystick.hats.size()/2, 0);
}
}
@@ -1856,6 +1861,25 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
}
return true;
}
+
+ public String getJoystickDescriptor(InputDevice joystickDevice) {
+ return joystickDevice.getName();
+ }
+}
+
+
+class SDLJoystickHandler_API16 extends SDLJoystickHandler_API12 {
+
+ @Override
+ public String getJoystickDescriptor(InputDevice joystickDevice) {
+ String desc = joystickDevice.getDescriptor();
+
+ if (desc != null && desc != "") {
+ return desc;
+ }
+
+ return super.getJoystickDescriptor(joystickDevice);
+ }
}
class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 6dd078f..926b684 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -84,7 +84,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeHat)(
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(nativeAddJoystick)(
JNIEnv* env, jclass jcls,
- jint device_id, jstring device_name, jint is_accelerometer,
+ jint device_id, jstring device_name, jstring device_desc, jint is_accelerometer,
jint nbuttons, jint naxes, jint nhats, jint nballs);
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(nativeRemoveJoystick)(
@@ -365,15 +365,17 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeHat)(
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(nativeAddJoystick)(
JNIEnv* env, jclass jcls,
- jint device_id, jstring device_name, jint is_accelerometer,
+ jint device_id, jstring device_name, jstring device_desc, jint is_accelerometer,
jint nbuttons, jint naxes, jint nhats, jint nballs)
{
int retval;
const char *name = (*env)->GetStringUTFChars(env, device_name, NULL);
+ const char *desc = (*env)->GetStringUTFChars(env, device_desc, NULL);
- retval = Android_AddJoystick(device_id, name, (SDL_bool) is_accelerometer, nbuttons, naxes, nhats, nballs);
+ retval = Android_AddJoystick(device_id, name, desc, (SDL_bool) is_accelerometer, nbuttons, naxes, nhats, nballs);
(*env)->ReleaseStringUTFChars(env, device_name, name);
+ (*env)->ReleaseStringUTFChars(env, device_desc, desc);
return retval;
}
diff --git a/src/core/android/SDL_android.h b/src/core/android/SDL_android.h
index aae7180..e0ae7b5 100644
--- a/src/core/android/SDL_android.h
+++ b/src/core/android/SDL_android.h
@@ -35,6 +35,7 @@ extern "C" {
/* Interface from the SDL library into the Android Java activity */
extern void Android_JNI_SetActivityTitle(const char *title);
extern void Android_JNI_SetOrientation(int w, int h, int resizable, const char *hint);
+
extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
extern void Android_JNI_ShowTextInput(SDL_Rect *inputRect);
extern void Android_JNI_HideTextInput(void);
diff --git a/src/joystick/android/SDL_sysjoystick.c b/src/joystick/android/SDL_sysjoystick.c
index f835842..59adb85 100644
--- a/src/joystick/android/SDL_sysjoystick.c
+++ b/src/joystick/android/SDL_sysjoystick.c
@@ -244,7 +244,7 @@ Android_OnHat(int device_id, int hat_id, int x, int y)
int
-Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
+Android_AddJoystick(int device_id, const char *name, const char *desc, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
{
SDL_JoystickGUID guid;
SDL_joylist_item *item;
@@ -255,7 +255,7 @@ Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer,
/* the GUID is just the first 16 chars of the name for now */
SDL_zero( guid );
- SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
+ SDL_memcpy( &guid, desc, SDL_min( sizeof(guid), SDL_strlen( desc) ) );
item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
if (item == NULL) {
@@ -356,7 +356,7 @@ SDL_SYS_JoystickInit(void)
if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
/* Default behavior, accelerometer as joystick */
- Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
+ Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
}
return (numjoysticks);
diff --git a/src/joystick/android/SDL_sysjoystick_c.h b/src/joystick/android/SDL_sysjoystick_c.h
index a934773..cae7aff 100644
--- a/src/joystick/android/SDL_sysjoystick_c.h
+++ b/src/joystick/android/SDL_sysjoystick_c.h
@@ -32,7 +32,7 @@ extern int Android_OnPadDown(int device_id, int keycode);
extern int Android_OnPadUp(int device_id, int keycode);
extern int Android_OnJoy(int device_id, int axisnum, float value);
extern int Android_OnHat(int device_id, int hat_id, int x, int y);
-extern int Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs);
+extern int Android_AddJoystick(int device_id, const char *name, const char *desc, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs);
extern int Android_RemoveJoystick(int device_id);
/* A linked list of available joysticks */