src/core/android/SDL_android.c
changeset 8057 801d84e26f91
parent 8055 3e2f230a6d62
child 8093 b43765095a6f
equal deleted inserted replaced
8056:abd9434c5d3e 8057:801d84e26f91
    32 #include "../../events/SDL_events_c.h"
    32 #include "../../events/SDL_events_c.h"
    33 #include "../../video/android/SDL_androidkeyboard.h"
    33 #include "../../video/android/SDL_androidkeyboard.h"
    34 #include "../../video/android/SDL_androidtouch.h"
    34 #include "../../video/android/SDL_androidtouch.h"
    35 #include "../../video/android/SDL_androidvideo.h"
    35 #include "../../video/android/SDL_androidvideo.h"
    36 #include "../../video/android/SDL_androidwindow.h"
    36 #include "../../video/android/SDL_androidwindow.h"
    37 #include "../../joystick/android/SDL_sysjoystick.h"
    37 #include "../../joystick/android/SDL_sysjoystick_c.h"
    38 
    38 
    39 #include <android/log.h>
    39 #include <android/log.h>
    40 #include <pthread.h>
    40 #include <pthread.h>
    41 #include <sys/types.h>
    41 #include <sys/types.h>
    42 #include <unistd.h>
    42 #include <unistd.h>
    73 static jmethodID midFlipBuffers;
    73 static jmethodID midFlipBuffers;
    74 static jmethodID midAudioInit;
    74 static jmethodID midAudioInit;
    75 static jmethodID midAudioWriteShortBuffer;
    75 static jmethodID midAudioWriteShortBuffer;
    76 static jmethodID midAudioWriteByteBuffer;
    76 static jmethodID midAudioWriteByteBuffer;
    77 static jmethodID midAudioQuit;
    77 static jmethodID midAudioQuit;
       
    78 static jmethodID midPollInputDevices;
    78 
    79 
    79 /* Accelerometer data storage */
    80 /* Accelerometer data storage */
    80 static float fLastAccelerometer[3];
    81 static float fLastAccelerometer[3];
    81 static bool bHasNewData;
    82 static bool bHasNewData;
    82 
    83 
   125                                 "audioWriteShortBuffer", "([S)V");
   126                                 "audioWriteShortBuffer", "([S)V");
   126     midAudioWriteByteBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
   127     midAudioWriteByteBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
   127                                 "audioWriteByteBuffer", "([B)V");
   128                                 "audioWriteByteBuffer", "([B)V");
   128     midAudioQuit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
   129     midAudioQuit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
   129                                 "audioQuit", "()V");
   130                                 "audioQuit", "()V");
       
   131     midPollInputDevices = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
       
   132                                 "pollInputDevices", "()V");
   130 
   133 
   131     bHasNewData = false;
   134     bHasNewData = false;
   132 
   135 
   133     if(!midGetNativeSurface || !midFlipBuffers || !midAudioInit ||
   136     if(!midGetNativeSurface || !midFlipBuffers || !midAudioInit ||
   134        !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
   137        !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit || !midPollInputDevices) {
   135         __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
   138         __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
   136     }
   139     }
   137     __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
   140     __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
   138 }
   141 }
   139 
   142 
   146 }
   149 }
   147 
   150 
   148 // Paddown
   151 // Paddown
   149 int Java_org_libsdl_app_SDLActivity_onNativePadDown(
   152 int Java_org_libsdl_app_SDLActivity_onNativePadDown(
   150                                     JNIEnv* env, jclass jcls,
   153                                     JNIEnv* env, jclass jcls,
   151                                     jint padId, jint keycode)
   154                                     jint device_id, jint keycode)
   152 {
   155 {
   153     return Android_OnPadDown(padId, keycode);
   156     return Android_OnPadDown(device_id, keycode);
   154 }
   157 }
   155 
   158 
   156 // Padup
   159 // Padup
   157 int Java_org_libsdl_app_SDLActivity_onNativePadUp(
   160 int Java_org_libsdl_app_SDLActivity_onNativePadUp(
   158                                    JNIEnv* env, jclass jcls,
   161                                    JNIEnv* env, jclass jcls,
   159                                    jint padId, jint keycode)
   162                                    jint device_id, jint keycode)
   160 {
   163 {
   161     return Android_OnPadUp(padId, keycode);
   164     return Android_OnPadUp(device_id, keycode);
   162 }
   165 }
   163 
   166 
   164 /* Joy */
   167 /* Joy */
   165 void Java_org_libsdl_app_SDLActivity_onNativeJoy(
   168 void Java_org_libsdl_app_SDLActivity_onNativeJoy(
   166                                     JNIEnv* env, jclass jcls,
   169                                     JNIEnv* env, jclass jcls,
   167                                     jint joyId, jint axis, jfloat value)
   170                                     jint device_id, jint axis, jfloat value)
   168 {
   171 {
   169     Android_OnJoy(joyId, axis, value);
   172     Android_OnJoy(device_id, axis, value);
       
   173 }
       
   174 
       
   175 
       
   176 int Java_org_libsdl_app_SDLActivity_nativeAddJoystick(
       
   177     JNIEnv* env, jclass jcls,
       
   178     jint device_id, jstring device_name, jint is_accelerometer, 
       
   179     jint nbuttons, jint naxes, jint nhats, jint nballs)
       
   180 {
       
   181     int retval;
       
   182     const char *name = (*env)->GetStringUTFChars(env, device_name, NULL);
       
   183 
       
   184     retval = Android_AddJoystick(device_id, name, (SDL_bool) is_accelerometer, nbuttons, naxes, nhats, nballs);
       
   185 
       
   186     (*env)->ReleaseStringUTFChars(env, device_name, name);
       
   187     
       
   188     return retval;
       
   189 }
       
   190 
       
   191 int Java_org_libsdl_app_SDLActivity_nativeRemoveJoystick(
       
   192     JNIEnv* env, jclass jcls, jint device_id)
       
   193 {
       
   194     return Android_RemoveJoystick(device_id);
   170 }
   195 }
   171 
   196 
   172 
   197 
   173 /* Surface Created */
   198 /* Surface Created */
   174 void Java_org_libsdl_app_SDLActivity_onNativeSurfaceChanged(JNIEnv* env, jclass jcls)
   199 void Java_org_libsdl_app_SDLActivity_onNativeSurfaceChanged(JNIEnv* env, jclass jcls)
  1245         (*env)->DeleteLocalRef(env, array);
  1270         (*env)->DeleteLocalRef(env, array);
  1246     }
  1271     }
  1247     return number;
  1272     return number;
  1248 }
  1273 }
  1249 
  1274 
  1250 /* return the total number of plugged in joysticks */
  1275 void Android_JNI_PollInputDevices()
  1251 int Android_JNI_GetNumJoysticks()
  1276 {
  1252 {
  1277     JNIEnv *env = Android_JNI_GetEnv();
  1253     JNIEnv* env = Android_JNI_GetEnv();
  1278     (*env)->CallStaticVoidMethod(env, mActivityClass, midPollInputDevices);    
  1254     if (!env) {
  1279 }
  1255         return -1;
       
  1256     }
       
  1257     
       
  1258     jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getNumJoysticks", "()I");
       
  1259     if (!mid) {
       
  1260         return -1;
       
  1261     }
       
  1262     
       
  1263     return (int)(*env)->CallStaticIntMethod(env, mActivityClass, mid);
       
  1264 }
       
  1265 
       
  1266 /* Return the name of joystick number "i" */
       
  1267 char* Android_JNI_GetJoystickName(int i)
       
  1268 {
       
  1269     JNIEnv* env = Android_JNI_GetEnv();
       
  1270     if (!env) {
       
  1271         return SDL_strdup("");
       
  1272     }
       
  1273     
       
  1274     jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getJoystickName", "(I)Ljava/lang/String;");
       
  1275     if (!mid) {
       
  1276         return SDL_strdup("");
       
  1277     }
       
  1278     jstring string = (jstring)((*env)->CallStaticObjectMethod(env, mActivityClass, mid, i));
       
  1279     const char* utf = (*env)->GetStringUTFChars(env, string, 0);
       
  1280     if (!utf) {
       
  1281         return SDL_strdup("");
       
  1282     }
       
  1283     
       
  1284     char* text = SDL_strdup(utf);
       
  1285     (*env)->ReleaseStringUTFChars(env, string, utf);
       
  1286     return text;
       
  1287 }
       
  1288 
       
  1289 /* return the number of axes in the given joystick */
       
  1290 int Android_JNI_GetJoystickAxes(int joy)
       
  1291 {
       
  1292     JNIEnv* env = Android_JNI_GetEnv();
       
  1293     if (!env) {
       
  1294         return -1;
       
  1295     }
       
  1296     
       
  1297     jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getJoystickAxes", "(I)I");
       
  1298     if (!mid) {
       
  1299         return -1;
       
  1300     }
       
  1301     
       
  1302     return (int)(*env)->CallIntMethod(env, mActivityClass, mid, joy);
       
  1303 }
       
  1304 
       
  1305 
  1280 
  1306 /* sends message to be handled on the UI event dispatch thread */
  1281 /* sends message to be handled on the UI event dispatch thread */
  1307 int Android_JNI_SendMessage(int command, int param)
  1282 int Android_JNI_SendMessage(int command, int param)
  1308 {
  1283 {
  1309     JNIEnv *env = Android_JNI_GetEnv();
  1284     JNIEnv *env = Android_JNI_GetEnv();