src/core/android/SDL_android.cpp
changeset 7018 9cef1005df5f
parent 6885 700f1b25f77f
child 7037 3fedf1f25b94
     1.1 --- a/src/core/android/SDL_android.cpp	Tue Mar 19 22:25:02 2013 -0700
     1.2 +++ b/src/core/android/SDL_android.cpp	Tue Mar 19 23:03:57 2013 -0700
     1.3 @@ -31,11 +31,15 @@
     1.4  
     1.5  extern "C" {
     1.6  #include "../../events/SDL_events_c.h"
     1.7 +#include "../../joystick/android/SDL_androidjoystick.h"
     1.8  #include "../../video/android/SDL_androidkeyboard.h"
     1.9  #include "../../video/android/SDL_androidtouch.h"
    1.10  #include "../../video/android/SDL_androidvideo.h"
    1.11  
    1.12  #include <android/log.h>
    1.13 +#if ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK
    1.14 +#include <android/sensor.h>
    1.15 +#endif
    1.16  #include <pthread.h>
    1.17  #include <sys/types.h>
    1.18  #include <unistd.h>
    1.19 @@ -76,9 +80,11 @@
    1.20  static jmethodID midAudioWriteByteBuffer;
    1.21  static jmethodID midAudioQuit;
    1.22  
    1.23 +#ifdef ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK
    1.24  // Accelerometer data storage
    1.25  static float fLastAccelerometer[3];
    1.26  static bool bHasNewData;
    1.27 +#endif
    1.28  
    1.29  /*******************************************************************************
    1.30                   Functions called by JNI
    1.31 @@ -130,7 +136,9 @@
    1.32      midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
    1.33                                  "audioQuit", "()V");
    1.34  
    1.35 +#ifdef ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK
    1.36      bHasNewData = false;
    1.37 +#endif
    1.38  
    1.39      if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
    1.40         !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
    1.41 @@ -147,6 +155,27 @@
    1.42      Android_SetScreenResolution(width, height, format);
    1.43  }
    1.44  
    1.45 +// Paddown
    1.46 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativePadDown(
    1.47 +                                    JNIEnv* env, jclass jcls, jint padId, jint keycode)
    1.48 +{
    1.49 +    Android_OnPadDown(padId, keycode);
    1.50 +}
    1.51 +
    1.52 +// Padup
    1.53 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativePadUp(
    1.54 +                                    JNIEnv* env, jclass jcls, jint padId, jint keycode)
    1.55 +{
    1.56 +    Android_OnPadUp(padId, keycode);
    1.57 +}
    1.58 +
    1.59 +// Joysticks
    1.60 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeJoy(
    1.61 +                                    JNIEnv* env, jclass jcls, jint joyId, jint axisNum, jfloat value)
    1.62 +{
    1.63 +    Android_OnJoy(joyId, axisNum, value);
    1.64 +}
    1.65 +
    1.66  // Keydown
    1.67  extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
    1.68                                      JNIEnv* env, jclass jcls, jint keycode)
    1.69 @@ -170,6 +199,7 @@
    1.70      Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p);
    1.71  }
    1.72  
    1.73 +#if ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK
    1.74  // Accelerometer
    1.75  extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
    1.76                                      JNIEnv* env, jclass jcls,
    1.77 @@ -180,6 +210,7 @@
    1.78      fLastAccelerometer[2] = z;
    1.79      bHasNewData = true;
    1.80  }
    1.81 +#endif
    1.82  
    1.83  // Quit
    1.84  extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
    1.85 @@ -347,6 +378,7 @@
    1.86      }
    1.87  }
    1.88  
    1.89 +#if ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK
    1.90  extern "C" SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
    1.91  {
    1.92      int i;
    1.93 @@ -362,6 +394,7 @@
    1.94  
    1.95      return retval;
    1.96  }
    1.97 +#endif
    1.98  
    1.99  static void Android_JNI_ThreadDestroyed(void* value) {
   1.100      /* The thread is being destroyed, detach it from the Java VM and set the mThreadKey value to NULL as required */
   1.101 @@ -1082,6 +1115,101 @@
   1.102      return 0;
   1.103  }
   1.104  
   1.105 +// Initialize the joystick subsystem on the Java side
   1.106 +int Android_JNI_JoystickInit()
   1.107 +{
   1.108 +    JNIEnv* env = Android_JNI_GetEnv();
   1.109 +    if (!env) {
   1.110 +        return -1;
   1.111 +    }
   1.112 +    jmethodID mid = env->GetStaticMethodID(mActivityClass, "joystickInit", "()V");
   1.113 +    if (!mid) {
   1.114 +        return -1;
   1.115 +    }
   1.116 +    env->CallStaticVoidMethod(mActivityClass, mid);
   1.117 +    return 0;
   1.118 +}
   1.119 +
   1.120 +// Quit the joystick subsystem on the Java side
   1.121 +int Android_JNI_JoystickQuit()
   1.122 +{
   1.123 +    JNIEnv* env = Android_JNI_GetEnv();
   1.124 +    if (!env) {
   1.125 +        return -1;
   1.126 +    }
   1.127 +    jmethodID mid = env->GetStaticMethodID(mActivityClass, "joystickQuit", "()V");
   1.128 +    if (!mid) {
   1.129 +        return -1;
   1.130 +    }
   1.131 +    env->CallStaticVoidMethod(mActivityClass, mid);
   1.132 +    return 0;
   1.133 +}
   1.134 +
   1.135 +// return the total number of plugged in joysticks
   1.136 +extern "C" int Android_JNI_GetNumJoysticks()
   1.137 +{
   1.138 +    JNIEnv* env = Android_JNI_GetEnv();
   1.139 +    if (!env) {
   1.140 +        return -1;
   1.141 +    }
   1.142 +    jmethodID mid = env->GetStaticMethodID(mActivityClass, "getNumJoysticks", "()I");
   1.143 +    if (!mid) {
   1.144 +        return -1;
   1.145 +    }
   1.146 +    
   1.147 +    return env->CallStaticIntMethod(mActivityClass, mid);
   1.148 +}
   1.149 +
   1.150 +// Return the name of joystick number "index"
   1.151 +extern "C" char* Android_JNI_GetJoystickName(int index)
   1.152 +{
   1.153 +    JNIEnv* env = Android_JNI_GetEnv();
   1.154 +    if (!env) {
   1.155 +        return SDL_strdup("");
   1.156 +    }
   1.157 +
   1.158 +    jmethodID mid = env->GetStaticMethodID(mActivityClass, "getJoystickName", "(I)Ljava/lang/String;");
   1.159 +    if (!mid) {
   1.160 +            return SDL_strdup("");
   1.161 +    }
   1.162 +    jstring string = reinterpret_cast<jstring>(env->CallStaticObjectMethod(mActivityClass, mid, index));
   1.163 +    const char* utf = env->GetStringUTFChars(string, 0);
   1.164 +    if (!utf) {
   1.165 +            return SDL_strdup("");
   1.166 +    }
   1.167 +
   1.168 +    char* text = SDL_strdup(utf);
   1.169 +    env->ReleaseStringUTFChars(string, utf);
   1.170 +    return text;
   1.171 +}
   1.172 +
   1.173 +// return the number of axes in the given joystick
   1.174 +extern "C" int Android_JNI_GetJoystickNumOfAxes(int index)
   1.175 +{
   1.176 +    JNIEnv* env = Android_JNI_GetEnv();
   1.177 +    if (!env) {
   1.178 +        return -1;
   1.179 +    }
   1.180 +    jmethodID mid = env->GetStaticMethodID(mActivityClass, "getJoystickNumOfAxes", "(I)I");
   1.181 +    if (!mid) {
   1.182 +        return -1;
   1.183 +    }
   1.184 +    
   1.185 +    return env->CallStaticIntMethod(mActivityClass, mid, index);
   1.186 +}
   1.187 +
   1.188 +#if ENABLE_ACCELOMETER_AS_EMULATED_JOYSTICK
   1.189 +// Return the name of the default accelerometer
   1.190 +// This is much easier to be done with NDK than with JNI
   1.191 +extern "C" char* Android_GetAccelName()
   1.192 +{
   1.193 +    ASensorManager* mSensorManager = ASensorManager_getInstance();
   1.194 +    ASensor const* mAccelerometer = ASensorManager_getDefaultSensor(mSensorManager, ASENSOR_TYPE_ACCELEROMETER);
   1.195 +
   1.196 +    return SDL_strdup(ASensor_getName(mAccelerometer));
   1.197 +}
   1.198 +#endif
   1.199 +
   1.200  // sends message to be handled on the UI event dispatch thread
   1.201  extern "C" int Android_JNI_SendMessage(int command, int param)
   1.202  {