Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
authorSam Lantinga <slouken@libsdl.org>
Fri, 13 Jan 2012 20:57:35 -0500
changeset 6212 78d854de3a66
parent 6211 b0b81f18bf35
child 6213 e50324ad38ee
Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop morgan.devel@gmail.com 2012-01-13 00:32:23 PST The android version of SDL_SYS_JoystickUpdate doesn't check if there is actually new data and always generate the SDL_JOYAXISMOTION event. Consequently, doing a while(SDL_PollEvent()) will result in an endless loop. The attached patch fix this issue. It also scale the incoming values properly in the Sint16 range. The scale from [-gravity;+gravity] is done directly in the java part because one may want to map the sensor values with a non-linear method for example.
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.cpp
src/core/android/SDL_android.h
src/joystick/android/SDL_sysjoystick.c
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Thu Jan 12 22:54:09 2012 -0500
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Fri Jan 13 20:57:35 2012 -0500
@@ -565,9 +565,9 @@
 
     public void onSensorChanged(SensorEvent event) {
         if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
-            SDLActivity.onNativeAccel(event.values[0],
-                                      event.values[1],
-                                      event.values[2]);
+            SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH,
+                                      event.values[1] / SensorManager.GRAVITY_EARTH,
+                                      event.values[2] / SensorManager.GRAVITY_EARTH);
         }
     }
 
--- a/src/core/android/SDL_android.cpp	Thu Jan 12 22:54:09 2012 -0500
+++ b/src/core/android/SDL_android.cpp	Fri Jan 13 20:57:35 2012 -0500
@@ -70,7 +70,7 @@
 
 // Accelerometer data storage
 static float fLastAccelerometer[3];
-
+static bool bHasNewData;
 
 /*******************************************************************************
                  Functions called by JNI
@@ -111,6 +111,8 @@
     midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
                                 "audioQuit", "()V");
 
+    bHasNewData = false;
+
     if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
        !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
         __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
@@ -156,7 +158,8 @@
 {
     fLastAccelerometer[0] = x;
     fLastAccelerometer[1] = y;
-    fLastAccelerometer[2] = z;   
+    fLastAccelerometer[2] = z;
+    bHasNewData = true;
 }
 
 // Quit
@@ -224,12 +227,20 @@
     }
 }
 
-extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
+extern "C" SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
 {
     int i;
-    for (i = 0; i < 3; ++i) {
-        values[i] = fLastAccelerometer[i];
+    SDL_bool retval = SDL_FALSE;
+
+    if (bHasNewData) {
+        for (i = 0; i < 3; ++i) {
+            values[i] = fLastAccelerometer[i];
+        }
+        bHasNewData = false;
+        retval = SDL_TRUE;
     }
+
+    return retval;
 }
 
 //
--- a/src/core/android/SDL_android.h	Thu Jan 12 22:54:09 2012 -0500
+++ b/src/core/android/SDL_android.h	Fri Jan 13 20:57:35 2012 -0500
@@ -31,7 +31,7 @@
 extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion);
 extern void Android_JNI_SwapWindow();
 extern void Android_JNI_SetActivityTitle(const char *title);
-extern void Android_JNI_GetAccelerometerValues(float values[3]);
+extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
 
 // Audio support
 extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);
--- a/src/joystick/android/SDL_sysjoystick.c	Thu Jan 12 22:54:09 2012 -0500
+++ b/src/joystick/android/SDL_sysjoystick.c	Fri Jan 13 20:57:35 2012 -0500
@@ -86,12 +86,14 @@
 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
 {
     int i;
+    Sint16 value;
     float values[3];
 
-    Android_JNI_GetAccelerometerValues(values);
-
-    for ( i = 0; i < 3; i++ ) {
-        SDL_PrivateJoystickAxis(joystick, i, values[i]);
+    if (Android_JNI_GetAccelerometerValues(values)) {
+        for ( i = 0; i < 3; i++ ) {
+            value = (Sint16)(values[i] * 32767.0f);
+            SDL_PrivateJoystickAxis(joystick, i, value);
+        }
     }
 }