src/core/android/SDL_android.cpp
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 21:31:32 -0800
changeset 5092 ed1d54f1290a
parent 5090 327f181542f1
child 5222 c66b2a778b7e
permissions -rw-r--r--
Fixed Android build
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_android.h"
    25 
    26 extern "C" {
    27 #include "../../events/SDL_events_c.h"
    28 #include "../../video/android/SDL_androidkeyboard.h"
    29 #include "../../video/android/SDL_androidtouch.h"
    30 #include "../../video/android/SDL_androidvideo.h"
    31 
    32 /* Impelemented in audio/android/SDL_androidaudio.c */
    33 extern void Android_RunAudioThread();
    34 } // C
    35 
    36 /*******************************************************************************
    37  This file links the Java side of Android with libsdl
    38 *******************************************************************************/
    39 #include <jni.h>
    40 #include <android/log.h>
    41 
    42 
    43 /*******************************************************************************
    44                                Globals
    45 *******************************************************************************/
    46 static JNIEnv* mEnv = NULL;
    47 static JNIEnv* mAudioEnv = NULL;
    48 
    49 // Main activity
    50 static jclass mActivityClass;
    51 
    52 // method signatures
    53 static jmethodID midCreateGLContext;
    54 static jmethodID midFlipBuffers;
    55 static jmethodID midAudioInit;
    56 static jmethodID midAudioWriteShortBuffer;
    57 static jmethodID midAudioWriteByteBuffer;
    58 static jmethodID midAudioQuit;
    59 
    60 // Accelerometer data storage
    61 static float fLastAccelerometer[3];
    62 
    63 
    64 /*******************************************************************************
    65                  Functions called by JNI
    66 *******************************************************************************/
    67 
    68 // Library init
    69 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
    70 {
    71     return JNI_VERSION_1_4;
    72 }
    73 
    74 // Called before SDL_main() to initialize JNI bindings
    75 extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
    76 {
    77     __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
    78 
    79     mEnv = env;
    80     mActivityClass = cls;
    81 
    82     midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
    83                                 "createGLContext","()V");
    84     midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
    85                                 "flipBuffers","()V");
    86     midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
    87                                 "audioInit", "(IZZI)Ljava/lang/Object;");
    88     midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
    89                                 "audioWriteShortBuffer", "([S)V");
    90     midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
    91                                 "audioWriteByteBuffer", "([B)V");
    92     midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
    93                                 "audioQuit", "()V");
    94 
    95     if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
    96        !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
    97         __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
    98     }
    99 }
   100 
   101 // Resize
   102 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
   103                                     JNIEnv* env, jclass jcls,
   104                                     jint width, jint height, jint format)
   105 {
   106     Android_SetScreenResolution(width, height, format);
   107 }
   108 
   109 // Keydown
   110 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
   111                                     JNIEnv* env, jclass jcls, jint keycode)
   112 {
   113     Android_OnKeyDown(keycode);
   114 }
   115 
   116 // Keyup
   117 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
   118                                     JNIEnv* env, jclass jcls, jint keycode)
   119 {
   120     Android_OnKeyUp(keycode);
   121 }
   122 
   123 // Touch
   124 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
   125                                     JNIEnv* env, jclass jcls,
   126                                     jint action, jfloat x, jfloat y, jfloat p)
   127 {
   128     Android_OnTouch(action, x, y, p);
   129 }
   130 
   131 // Accelerometer
   132 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
   133                                     JNIEnv* env, jclass jcls,
   134                                     jfloat x, jfloat y, jfloat z)
   135 {
   136     fLastAccelerometer[0] = x;
   137     fLastAccelerometer[1] = y;
   138     fLastAccelerometer[2] = z;   
   139 }
   140 
   141 // Quit
   142 extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
   143                                     JNIEnv* env, jclass cls)
   144 {    
   145     // Inject a SDL_QUIT event
   146     SDL_SendQuit();
   147 }
   148 
   149 extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
   150                                     JNIEnv* env, jclass cls)
   151 {
   152     /* This is the audio thread, with a different environment */
   153     mAudioEnv = env;
   154 
   155     Android_RunAudioThread();
   156 }
   157 
   158 
   159 /*******************************************************************************
   160              Functions called by SDL into Java
   161 *******************************************************************************/
   162 extern "C" void Android_JNI_CreateContext()
   163 {
   164     mEnv->CallStaticVoidMethod(mActivityClass, midCreateGLContext); 
   165 }
   166 
   167 extern "C" void Android_JNI_SwapWindow()
   168 {
   169     mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers); 
   170 }
   171 
   172 extern "C" void Android_JNI_SetActivityTitle(const char *title)
   173 {
   174     jmethodID mid;
   175 
   176     mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
   177     if (mid) {
   178         mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title));
   179     }
   180 }
   181 
   182 extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
   183 {
   184     int i;
   185     for (i = 0; i < 3; ++i) {
   186         values[i] = fLastAccelerometer[i];
   187     }
   188 }
   189 
   190 //
   191 // Audio support
   192 //
   193 static jboolean audioBuffer16Bit = JNI_FALSE;
   194 static jboolean audioBufferStereo = JNI_FALSE;
   195 static jobject audioBuffer = NULL;
   196 static void* audioBufferPinned = NULL;
   197 
   198 extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
   199 {
   200     int audioBufferFrames;
   201 
   202     __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
   203     audioBuffer16Bit = is16Bit;
   204     audioBufferStereo = channelCount > 1;
   205 
   206     audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
   207 
   208     if (audioBuffer == NULL) {
   209         __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
   210         return 0;
   211     }
   212     audioBuffer = mEnv->NewGlobalRef(audioBuffer);
   213 
   214     jboolean isCopy = JNI_FALSE;
   215     if (audioBuffer16Bit) {
   216         audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
   217         audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
   218     } else {
   219         audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
   220         audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
   221     }
   222     if (audioBufferStereo) {
   223         audioBufferFrames /= 2;
   224     }
   225 
   226     return audioBufferFrames;
   227 }
   228 
   229 extern "C" void * Android_JNI_GetAudioBuffer()
   230 {
   231     return audioBufferPinned;
   232 }
   233 
   234 extern "C" void Android_JNI_WriteAudioBuffer()
   235 {
   236     if (audioBuffer16Bit) {
   237         mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
   238         mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
   239     } else {
   240         mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
   241         mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
   242     }
   243 
   244     /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
   245 }
   246 
   247 extern "C" void Android_JNI_CloseAudioDevice()
   248 {
   249     mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit); 
   250 
   251     if (audioBuffer) {
   252         mEnv->DeleteGlobalRef(audioBuffer);
   253         audioBuffer = NULL;
   254         audioBufferPinned = NULL;
   255     }
   256 }
   257 
   258 /* vi: set ts=4 sw=4 expandtab: */