diff -r a500a9dbfb41 Android.mk --- a/Android.mk Tue Mar 19 23:24:24 2013 -0700 +++ b/Android.mk Wed Mar 20 12:25:44 2013 +0200 @@ -42,6 +42,6 @@ $(wildcard $(LOCAL_PATH)/src/video/android/*.c)) LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES -LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog +LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid include $(BUILD_SHARED_LIBRARY) diff -r a500a9dbfb41 android-project/src/org/libsdl/app/SDLActivity.java --- a/android-project/src/org/libsdl/app/SDLActivity.java Tue Mar 19 23:24:24 2013 -0700 +++ b/android-project/src/org/libsdl/app/SDLActivity.java Wed Mar 20 12:25:44 2013 +0200 @@ -171,7 +171,7 @@ public static native void onNativeTouch(int touchDevId, int pointerFingerId, int action, float x, float y, float p); -// public static native void onNativeAccel(float x, float y, float z); + public static native void onNativeAccel(float x, float y, float z); public static native void nativeRunAudioThread(); @@ -764,13 +764,11 @@ } public void onSensorChanged(SensorEvent event) { -/* if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH, event.values[1] / SensorManager.GRAVITY_EARTH, event.values[2] / SensorManager.GRAVITY_EARTH); } -*/ } } diff -r a500a9dbfb41 src/core/android/SDL_android.cpp --- a/src/core/android/SDL_android.cpp Tue Mar 19 23:24:24 2013 -0700 +++ b/src/core/android/SDL_android.cpp Wed Mar 20 12:25:44 2013 +0200 @@ -37,9 +37,7 @@ #include "../../video/android/SDL_androidvideo.h" #include -#if ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK #include -#endif #include #include #include @@ -80,11 +78,9 @@ static jmethodID midAudioWriteByteBuffer; static jmethodID midAudioQuit; -#ifdef ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK // Accelerometer data storage static float fLastAccelerometer[3]; static bool bHasNewData; -#endif /******************************************************************************* Functions called by JNI @@ -136,9 +132,7 @@ midAudioQuit = mEnv->GetStaticMethodID(mActivityClass, "audioQuit", "()V"); -#ifdef ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK bHasNewData = false; -#endif if(!midCreateGLContext || !midFlipBuffers || !midAudioInit || !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) { @@ -199,7 +193,6 @@ Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p); } -#if ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK // Accelerometer extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel( JNIEnv* env, jclass jcls, @@ -210,7 +203,6 @@ fLastAccelerometer[2] = z; bHasNewData = true; } -#endif // Quit extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( @@ -378,7 +370,6 @@ } } -#if ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK extern "C" SDL_bool Android_JNI_GetAccelerometerValues(float values[3]) { int i; @@ -394,7 +385,6 @@ return retval; } -#endif static void Android_JNI_ThreadDestroyed(void* value) { /* The thread is being destroyed, detach it from the Java VM and set the mThreadKey value to NULL as required */ @@ -1198,7 +1188,6 @@ return env->CallStaticIntMethod(mActivityClass, mid, index); } -#if ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK // Return the name of the default accelerometer // This is much easier to be done with NDK than with JNI extern "C" char* Android_GetAccelName() @@ -1208,7 +1197,6 @@ return SDL_strdup(ASensor_getName(mAccelerometer)); } -#endif // sends message to be handled on the UI event dispatch thread extern "C" int Android_JNI_SendMessage(int command, int param) diff -r a500a9dbfb41 src/core/android/SDL_android.h --- a/src/core/android/SDL_android.h Tue Mar 19 23:24:24 2013 -0700 +++ b/src/core/android/SDL_android.h Wed Mar 20 12:25:44 2013 +0200 @@ -33,9 +33,7 @@ extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion, int red, int green, int blue, int alpha, int buffer, int depth, int stencil, int buffers, int samples); extern void Android_JNI_SwapWindow(); extern void Android_JNI_SetActivityTitle(const char *title); -#ifdef ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]); -#endif extern void Android_JNI_ShowTextInput(SDL_Rect *inputRect); extern void Android_JNI_HideTextInput(); @@ -68,9 +66,7 @@ int Android_JNI_GetNumJoysticks(); char* Android_JNI_GetJoystickName(int i); int Android_JNI_GetJoystickNumOfAxes(int index); -#ifdef ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK char* Android_GetAccelName(); -#endif // Threads #include diff -r a500a9dbfb41 src/joystick/android/SDL_androidjoystick.c --- a/src/joystick/android/SDL_androidjoystick.c Tue Mar 19 23:24:24 2013 -0700 +++ b/src/joystick/android/SDL_androidjoystick.c Wed Mar 20 12:25:44 2013 +0200 @@ -75,10 +75,11 @@ int SDL_SYS_JoystickInit(void) { - int i = 0; + int i; if (Android_JNI_JoystickInit() < 0) return (-1); - SYS_numjoysticks = Android_JNI_GetNumJoysticks(); + // The latest entry is for the accelerometer + SYS_numjoysticks = Android_JNI_GetNumJoysticks()+1; SYS_Joysticks = (SDL_Joystick **)SDL_malloc(SYS_numjoysticks*sizeof(SDL_Joystick *)); if (SYS_Joysticks == NULL) { @@ -96,10 +97,11 @@ SDL_memset(SYS_JoystickNames, 0, (SYS_numjoysticks*sizeof(char *))); SDL_memset(SYS_Joysticks, 0, (SYS_numjoysticks*sizeof(SDL_Joystick *))); - for (i = 0; i < SYS_numjoysticks; i++) + for (i = 0; i < (SYS_numjoysticks-1); i++) { SYS_JoystickNames[i] = Android_JNI_GetJoystickName(i); } + SYS_JoystickNames[i] = Android_GetAccelName(); return (SYS_numjoysticks); } @@ -139,7 +141,8 @@ int SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) { - if( device_index < SYS_numjoysticks ) + // Handle the accelerometer separately + if( device_index < (SYS_numjoysticks-1) ) { // TODO: How to get the rest of the info?? // 36 is the maximum number of handled buttons @@ -148,6 +151,13 @@ joystick->nballs = 0; joystick->naxes = Android_JNI_GetJoystickNumOfAxes(device_index); } + else if( device_index == (SYS_numjoysticks-1) ) + { + joystick->nbuttons = 0; + joystick->nhats = 0; + joystick->nballs = 0; + joystick->naxes = 3; + } else { return -1; @@ -168,6 +178,21 @@ void SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) { + int i; + Sint16 value; + float values[3]; + + if( joystick->instance_id == (SYS_numjoysticks-1) ) + { + if (Android_JNI_GetAccelerometerValues(values)) + { + for ( i = 0; i < 3; i++ ) + { + value = (Sint16)(values[i] * 32767.0f); + SDL_PrivateJoystickAxis(joystick, i, value); + } + } + } } /* Function to determine is this joystick is attached to the system right now */