Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Browse files Browse the repository at this point in the history
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 24a658b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
6 changes: 3 additions & 3 deletions android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -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);
}
}

Expand Down
21 changes: 16 additions & 5 deletions src/core/android/SDL_android.cpp
Expand Up @@ -70,7 +70,7 @@ static jmethodID midAudioQuit;

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

static bool bHasNewData;

/*******************************************************************************
Functions called by JNI
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

//
Expand Down
2 changes: 1 addition & 1 deletion src/core/android/SDL_android.h
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions src/joystick/android/SDL_sysjoystick.c
Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 24a658b

Please sign in to comment.