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.
This commit is contained in:
Sam Lantinga 2012-01-13 20:57:35 -05:00
parent 3fe05cfb55
commit 40c6294290
4 changed files with 26 additions and 13 deletions

View file

@ -565,9 +565,9 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
public void onSensorChanged(SensorEvent event) { public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
SDLActivity.onNativeAccel(event.values[0], SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH,
event.values[1], event.values[1] / SensorManager.GRAVITY_EARTH,
event.values[2]); event.values[2] / SensorManager.GRAVITY_EARTH);
} }
} }

View file

@ -70,7 +70,7 @@ static jmethodID midAudioQuit;
// Accelerometer data storage // Accelerometer data storage
static float fLastAccelerometer[3]; static float fLastAccelerometer[3];
static bool bHasNewData;
/******************************************************************************* /*******************************************************************************
Functions called by JNI Functions called by JNI
@ -111,6 +111,8 @@ extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
midAudioQuit = mEnv->GetStaticMethodID(mActivityClass, midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
"audioQuit", "()V"); "audioQuit", "()V");
bHasNewData = false;
if(!midCreateGLContext || !midFlipBuffers || !midAudioInit || if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
!midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) { !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly"); __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 @@ extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
{ {
fLastAccelerometer[0] = x; fLastAccelerometer[0] = x;
fLastAccelerometer[1] = y; fLastAccelerometer[1] = y;
fLastAccelerometer[2] = z; fLastAccelerometer[2] = z;
bHasNewData = true;
} }
// Quit // Quit
@ -224,12 +227,20 @@ extern "C" void Android_JNI_SetActivityTitle(const char *title)
} }
} }
extern "C" void Android_JNI_GetAccelerometerValues(float values[3]) extern "C" SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
{ {
int i; int i;
for (i = 0; i < 3; ++i) { SDL_bool retval = SDL_FALSE;
values[i] = fLastAccelerometer[i];
if (bHasNewData) {
for (i = 0; i < 3; ++i) {
values[i] = fLastAccelerometer[i];
}
bHasNewData = false;
retval = SDL_TRUE;
} }
return retval;
} }
// //

View file

@ -31,7 +31,7 @@ extern "C" {
extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion); extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion);
extern void Android_JNI_SwapWindow(); extern void Android_JNI_SwapWindow();
extern void Android_JNI_SetActivityTitle(const char *title); 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 // Audio support
extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames); extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);

View file

@ -86,12 +86,14 @@ void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{ {
int i; int i;
Sint16 value;
float values[3]; float values[3];
Android_JNI_GetAccelerometerValues(values); if (Android_JNI_GetAccelerometerValues(values)) {
for ( i = 0; i < 3; i++ ) {
for ( i = 0; i < 3; i++ ) { value = (Sint16)(values[i] * 32767.0f);
SDL_PrivateJoystickAxis(joystick, i, values[i]); SDL_PrivateJoystickAxis(joystick, i, value);
}
} }
} }