From b7733367a3696014c6f391207e4723e6f1bf3029 Mon Sep 17 00:00:00 2001 From: Sergii Pylypenko Date: Tue, 14 Jul 2015 21:28:45 +0300 Subject: [PATCH] Added orientation sensor support --- changeAppSettings.sh | 10 ++++++ project/java/Accelerometer.java | 33 +++++++++++++++++-- project/java/Globals.java | 1 + .../src/video/android/SDL_androidinput.c | 19 +++++++++-- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/changeAppSettings.sh b/changeAppSettings.sh index c57b17a2d..124659d6b 100755 --- a/changeAppSettings.sh +++ b/changeAppSettings.sh @@ -258,6 +258,9 @@ echo >> AndroidAppSettings.cfg echo "# Application uses gyroscope (y) or (n), the gyroscope will be used as joystick 1 axes 2-4" >> AndroidAppSettings.cfg echo AppUsesGyroscope=$AppUsesGyroscope >> AndroidAppSettings.cfg echo >> AndroidAppSettings.cfg +echo "# Application uses orientation sensor (y) or (n), reported as joystick 1 axes 8-10" >> AndroidAppSettings.cfg +echo AppUsesOrientationSensor=$AppUsesOrientationSensor >> AndroidAppSettings.cfg +echo >> AndroidAppSettings.cfg echo "# Use gyroscope to move mouse cursor (y) or (n), it eats battery, and can be disabled in settings, do not use with AppUsesGyroscope setting" >> AndroidAppSettings.cfg echo MoveMouseWithGyroscope=$MoveMouseWithGyroscope >> AndroidAppSettings.cfg echo >> AndroidAppSettings.cfg @@ -595,6 +598,12 @@ else AppUsesGyroscope=false fi +if [ "$AppUsesOrientationSensor" = "y" ] ; then + AppUsesOrientationSensor=true +else + AppUsesOrientationSensor=false +fi + if [ "$MoveMouseWithGyroscope" = "y" ] ; then MoveMouseWithGyroscope=true else @@ -805,6 +814,7 @@ $SEDI "s/public static boolean AppUsesSecondJoystick = .*;/public static boolean $SEDI "s/public static boolean AppUsesThirdJoystick = .*;/public static boolean AppUsesThirdJoystick = $AppUsesThirdJoystick;/" project/src/Globals.java $SEDI "s/public static boolean AppUsesAccelerometer = .*;/public static boolean AppUsesAccelerometer = $AppUsesAccelerometer;/" project/src/Globals.java $SEDI "s/public static boolean AppUsesGyroscope = .*;/public static boolean AppUsesGyroscope = $AppUsesGyroscope;/" project/src/Globals.java +$SEDI "s/public static boolean AppUsesOrientationSensor = .*;/public static boolean AppUsesOrientationSensor = $AppUsesOrientationSensor;/" project/src/Globals.java $SEDI "s/public static boolean MoveMouseWithGyroscope = .*;/public static boolean MoveMouseWithGyroscope = $MoveMouseWithGyroscope;/" project/src/Globals.java $SEDI "s/public static boolean AppUsesMultitouch = .*;/public static boolean AppUsesMultitouch = $AppUsesMultitouch;/" project/src/Globals.java $SEDI "s/public static boolean NonBlockingSwapBuffers = .*;/public static boolean NonBlockingSwapBuffers = $NonBlockingSwapBuffers;/" project/src/Globals.java diff --git a/project/java/Accelerometer.java b/project/java/Accelerometer.java index cbcb376b2..3e93b82e9 100644 --- a/project/java/Accelerometer.java +++ b/project/java/Accelerometer.java @@ -35,6 +35,7 @@ import android.hardware.Sensor; import android.hardware.SensorEvent; import android.util.Log; import android.widget.TextView; +import android.os.Build; class AccelerometerReader implements SensorEventListener @@ -43,6 +44,7 @@ class AccelerometerReader implements SensorEventListener private SensorManager _manager = null; public boolean openedBySDL = false; public static final GyroscopeListener gyro = new GyroscopeListener(); + public static final OrientationListener orientation = new OrientationListener(); public AccelerometerReader(Activity context) { @@ -53,9 +55,10 @@ class AccelerometerReader implements SensorEventListener { if( _manager != null ) { - Log.i("SDL", "libSDL: stopping accelerometer/gyroscope"); + Log.i("SDL", "libSDL: stopping accelerometer/gyroscope/orientation"); _manager.unregisterListener(this); _manager.unregisterListener(gyro); + _manager.unregisterListener(orientation); } } @@ -73,6 +76,14 @@ class AccelerometerReader implements SensorEventListener Log.i("SDL", "libSDL: starting gyroscope"); _manager.registerListener(gyro, _manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME); } + if( (Globals.AppUsesOrientationSensor) && _manager != null && + _manager.getDefaultSensor(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? Sensor.TYPE_GAME_ROTATION_VECTOR : Sensor.TYPE_ROTATION_VECTOR) != null ) + { + Log.i("SDL", "libSDL: starting orientation sensor"); + _manager.registerListener(orientation, _manager.getDefaultSensor( + Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? Sensor.TYPE_GAME_ROTATION_VECTOR : Sensor.TYPE_ROTATION_VECTOR), + SensorManager.SENSOR_DELAY_GAME); + } } public void onSensorChanged(SensorEvent event) @@ -129,7 +140,10 @@ class AccelerometerReader implements SensorEventListener SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); if ( manager == null && manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) == null ) return; - manager.registerListener(l, manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME); + manager.registerListener(gyro, manager.getDefaultSensor( + Globals.AppUsesOrientationSensor ? Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? + Sensor.TYPE_GAME_ROTATION_VECTOR : Sensor.TYPE_ROTATION_VECTOR : Sensor.TYPE_GYROSCOPE), + SensorManager.SENSOR_DELAY_GAME); } public void unregisterListener(Activity context,SensorEventListener l) { @@ -140,6 +154,21 @@ class AccelerometerReader implements SensorEventListener } } + static class OrientationListener implements SensorEventListener + { + public OrientationListener() + { + } + public void onSensorChanged(SensorEvent event) + { + nativeOrientation(event.values[0], event.values[1], event.values[2]); + } + public void onAccuracyChanged(Sensor s, int a) + { + } + } + private static native void nativeAccelerometer(float accX, float accY, float accZ); private static native void nativeGyroscope(float X, float Y, float Z); + private static native void nativeOrientation(float X, float Y, float Z); } diff --git a/project/java/Globals.java b/project/java/Globals.java index b58f2077e..8760c1ba6 100644 --- a/project/java/Globals.java +++ b/project/java/Globals.java @@ -61,6 +61,7 @@ class Globals public static boolean AppUsesThirdJoystick = false; public static boolean AppUsesAccelerometer = false; public static boolean AppUsesGyroscope = false; + public static boolean AppUsesOrientationSensor = false; public static boolean AppUsesMultitouch = false; public static boolean NonBlockingSwapBuffers = false; public static boolean ResetSdlConfigForThisVersion = false; diff --git a/project/jni/sdl-1.2/src/video/android/SDL_androidinput.c b/project/jni/sdl-1.2/src/video/android/SDL_androidinput.c index 987ebecbc..06eb5a7d2 100644 --- a/project/jni/sdl-1.2/src/video/android/SDL_androidinput.c +++ b/project/jni/sdl-1.2/src/video/android/SDL_androidinput.c @@ -1063,7 +1063,6 @@ JAVA_EXPORT_NAME(AccelerometerReader_nativeAccelerometer) ( JNIEnv* env, jobjec SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 7, fminf(32767.0f, fmaxf(-32767.0f, accPosZ*1000.0f))); } - JNIEXPORT void JNICALL JAVA_EXPORT_NAME(AccelerometerReader_nativeGyroscope) ( JNIEnv* env, jobject thiz, jfloat X, jfloat Y, jfloat Z ) { @@ -1099,6 +1098,22 @@ JAVA_EXPORT_NAME(AccelerometerReader_nativeGyroscope) ( JNIEnv* env, jobject th } } +JNIEXPORT void JNICALL +JAVA_EXPORT_NAME(AccelerometerReader_nativeOrientation) ( JNIEnv* env, jobject thiz, jfloat X, jfloat Y, jfloat Z ) +{ +#if SDL_VERSION_ATLEAST(1,3,0) +#else + if( !SDL_CurrentVideoSurface ) + return; +#endif + + //__android_log_print(ANDROID_LOG_INFO, "libSDL", "Orientation %f %f %f", X, Y, Z); + + SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 8, NORMALIZE_FLOAT_32767(X)); + SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 9, NORMALIZE_FLOAT_32767(Y)); + SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 10, NORMALIZE_FLOAT_32767(Z)); +} + static int getClickTimeout(int v) { switch(v) @@ -1489,7 +1504,7 @@ int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) } if( joystick->index == JOY_ACCELGYRO ) { - joystick->naxes = 8; // Normalized accelerometer = axes 0-1, gyroscope = axes 2-4, raw accelerometer = axes 5-7 + joystick->naxes = 11; // Normalized accelerometer = axes 0-1, gyroscope = axes 2-4, raw accelerometer = axes 5-7, orientation = axes 8-10 if( !moveMouseWithGyroscope ) SDL_ANDROID_CallJavaStartAccelerometerGyroscope(1); }