Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

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.
  • Loading branch information
slouken committed Jan 14, 2012
1 parent cad3721 commit 24a658ba3074305f6f7824c00be183f21aa0674f
@@ -565,9 +565,9 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {

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);
}
}

@@ -70,7 +70,7 @@ static jmethodID midAudioQuit;

// Accelerometer data storage
static float fLastAccelerometer[3];

static bool bHasNewData;

/*******************************************************************************
Functions called by JNI
@@ -111,6 +111,8 @@ extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
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 @@ extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
{
fLastAccelerometer[0] = x;
fLastAccelerometer[1] = y;
fLastAccelerometer[2] = z;
fLastAccelerometer[2] = z;
bHasNewData = true;
}

// 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;
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;
}

//
@@ -31,7 +31,7 @@ extern "C" {
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);
@@ -86,12 +86,14 @@ void
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);
}
}
}

0 comments on commit 24a658b

Please sign in to comment.