Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
authorSam Lantinga <slouken@libsdl.org>
Fri, 13 Jan 2012 20:57:35 -0500
changeset 621278d854de3a66
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
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Thu Jan 12 22:54:09 2012 -0500
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Fri Jan 13 20:57:35 2012 -0500
     1.3 @@ -565,9 +565,9 @@
     1.4  
     1.5      public void onSensorChanged(SensorEvent event) {
     1.6          if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
     1.7 -            SDLActivity.onNativeAccel(event.values[0],
     1.8 -                                      event.values[1],
     1.9 -                                      event.values[2]);
    1.10 +            SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH,
    1.11 +                                      event.values[1] / SensorManager.GRAVITY_EARTH,
    1.12 +                                      event.values[2] / SensorManager.GRAVITY_EARTH);
    1.13          }
    1.14      }
    1.15  
     2.1 --- a/src/core/android/SDL_android.cpp	Thu Jan 12 22:54:09 2012 -0500
     2.2 +++ b/src/core/android/SDL_android.cpp	Fri Jan 13 20:57:35 2012 -0500
     2.3 @@ -70,7 +70,7 @@
     2.4  
     2.5  // Accelerometer data storage
     2.6  static float fLastAccelerometer[3];
     2.7 -
     2.8 +static bool bHasNewData;
     2.9  
    2.10  /*******************************************************************************
    2.11                   Functions called by JNI
    2.12 @@ -111,6 +111,8 @@
    2.13      midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
    2.14                                  "audioQuit", "()V");
    2.15  
    2.16 +    bHasNewData = false;
    2.17 +
    2.18      if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
    2.19         !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
    2.20          __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
    2.21 @@ -156,7 +158,8 @@
    2.22  {
    2.23      fLastAccelerometer[0] = x;
    2.24      fLastAccelerometer[1] = y;
    2.25 -    fLastAccelerometer[2] = z;   
    2.26 +    fLastAccelerometer[2] = z;
    2.27 +    bHasNewData = true;
    2.28  }
    2.29  
    2.30  // Quit
    2.31 @@ -224,12 +227,20 @@
    2.32      }
    2.33  }
    2.34  
    2.35 -extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
    2.36 +extern "C" SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
    2.37  {
    2.38      int i;
    2.39 -    for (i = 0; i < 3; ++i) {
    2.40 -        values[i] = fLastAccelerometer[i];
    2.41 +    SDL_bool retval = SDL_FALSE;
    2.42 +
    2.43 +    if (bHasNewData) {
    2.44 +        for (i = 0; i < 3; ++i) {
    2.45 +            values[i] = fLastAccelerometer[i];
    2.46 +        }
    2.47 +        bHasNewData = false;
    2.48 +        retval = SDL_TRUE;
    2.49      }
    2.50 +
    2.51 +    return retval;
    2.52  }
    2.53  
    2.54  //
     3.1 --- a/src/core/android/SDL_android.h	Thu Jan 12 22:54:09 2012 -0500
     3.2 +++ b/src/core/android/SDL_android.h	Fri Jan 13 20:57:35 2012 -0500
     3.3 @@ -31,7 +31,7 @@
     3.4  extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion);
     3.5  extern void Android_JNI_SwapWindow();
     3.6  extern void Android_JNI_SetActivityTitle(const char *title);
     3.7 -extern void Android_JNI_GetAccelerometerValues(float values[3]);
     3.8 +extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
     3.9  
    3.10  // Audio support
    3.11  extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);
     4.1 --- a/src/joystick/android/SDL_sysjoystick.c	Thu Jan 12 22:54:09 2012 -0500
     4.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Fri Jan 13 20:57:35 2012 -0500
     4.3 @@ -86,12 +86,14 @@
     4.4  SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
     4.5  {
     4.6      int i;
     4.7 +    Sint16 value;
     4.8      float values[3];
     4.9  
    4.10 -    Android_JNI_GetAccelerometerValues(values);
    4.11 -
    4.12 -    for ( i = 0; i < 3; i++ ) {
    4.13 -        SDL_PrivateJoystickAxis(joystick, i, values[i]);
    4.14 +    if (Android_JNI_GetAccelerometerValues(values)) {
    4.15 +        for ( i = 0; i < 3; i++ ) {
    4.16 +            value = (Sint16)(values[i] * 32767.0f);
    4.17 +            SDL_PrivateJoystickAxis(joystick, i, value);
    4.18 +        }
    4.19      }
    4.20  }
    4.21