src/core/android/SDL_android.cpp
changeset 5090 327f181542f1
parent 5001 77df56570442
child 5092 ed1d54f1290a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/core/android/SDL_android.cpp	Mon Jan 24 21:20:30 2011 -0800
     1.3 @@ -0,0 +1,258 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#include "SDL_android.h"
    1.28 +
    1.29 +extern "C" {
    1.30 +#include "events/SDL_events_c.h"
    1.31 +#include "video/android/SDL_androidkeyboard.h"
    1.32 +#include "video/android/SDL_androidtouch.h"
    1.33 +#include "video/android/SDL_androidvideo.h"
    1.34 +
    1.35 +/* Impelemented in audio/android/SDL_androidaudio.c */
    1.36 +extern void Android_RunAudioThread();
    1.37 +} // C
    1.38 +
    1.39 +/*******************************************************************************
    1.40 + This file links the Java side of Android with libsdl
    1.41 +*******************************************************************************/
    1.42 +#include <jni.h>
    1.43 +#include <android/log.h>
    1.44 +
    1.45 +
    1.46 +/*******************************************************************************
    1.47 +                               Globals
    1.48 +*******************************************************************************/
    1.49 +static JNIEnv* mEnv = NULL;
    1.50 +static JNIEnv* mAudioEnv = NULL;
    1.51 +
    1.52 +// Main activity
    1.53 +static jclass mActivityClass;
    1.54 +
    1.55 +// method signatures
    1.56 +static jmethodID midCreateGLContext;
    1.57 +static jmethodID midFlipBuffers;
    1.58 +static jmethodID midAudioInit;
    1.59 +static jmethodID midAudioWriteShortBuffer;
    1.60 +static jmethodID midAudioWriteByteBuffer;
    1.61 +static jmethodID midAudioQuit;
    1.62 +
    1.63 +// Accelerometer data storage
    1.64 +static float fLastAccelerometer[3];
    1.65 +
    1.66 +
    1.67 +/*******************************************************************************
    1.68 +                 Functions called by JNI
    1.69 +*******************************************************************************/
    1.70 +
    1.71 +// Library init
    1.72 +extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
    1.73 +{
    1.74 +    return JNI_VERSION_1_4;
    1.75 +}
    1.76 +
    1.77 +// Called before SDL_main() to initialize JNI bindings
    1.78 +extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
    1.79 +{
    1.80 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
    1.81 +
    1.82 +    mEnv = env;
    1.83 +    mActivityClass = cls;
    1.84 +
    1.85 +    midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
    1.86 +                                "createGLContext","()V");
    1.87 +    midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
    1.88 +                                "flipBuffers","()V");
    1.89 +    midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
    1.90 +                                "audioInit", "(IZZI)Ljava/lang/Object;");
    1.91 +    midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
    1.92 +                                "audioWriteShortBuffer", "([S)V");
    1.93 +    midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
    1.94 +                                "audioWriteByteBuffer", "([B)V");
    1.95 +    midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
    1.96 +                                "audioQuit", "()V");
    1.97 +
    1.98 +    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
    1.99 +       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
   1.100 +        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
   1.101 +    }
   1.102 +}
   1.103 +
   1.104 +// Resize
   1.105 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
   1.106 +                                    JNIEnv* env, jclass jcls,
   1.107 +                                    jint width, jint height, jint format)
   1.108 +{
   1.109 +    Android_SetScreenResolution(width, height, format);
   1.110 +}
   1.111 +
   1.112 +// Keydown
   1.113 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
   1.114 +                                    JNIEnv* env, jclass jcls, jint keycode)
   1.115 +{
   1.116 +    Android_OnKeyDown(keycode);
   1.117 +}
   1.118 +
   1.119 +// Keyup
   1.120 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
   1.121 +                                    JNIEnv* env, jclass jcls, jint keycode)
   1.122 +{
   1.123 +    Android_OnKeyUp(keycode);
   1.124 +}
   1.125 +
   1.126 +// Touch
   1.127 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
   1.128 +                                    JNIEnv* env, jclass jcls,
   1.129 +                                    jint action, jfloat x, jfloat y, jfloat p)
   1.130 +{
   1.131 +    Android_OnTouch(action, x, y, p);
   1.132 +}
   1.133 +
   1.134 +// Accelerometer
   1.135 +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
   1.136 +                                    JNIEnv* env, jclass jcls,
   1.137 +                                    jfloat x, jfloat y, jfloat z)
   1.138 +{
   1.139 +    fLastAccelerometer[0] = x;
   1.140 +    fLastAccelerometer[1] = y;
   1.141 +    fLastAccelerometer[2] = z;   
   1.142 +}
   1.143 +
   1.144 +// Quit
   1.145 +extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
   1.146 +                                    JNIEnv* env, jclass cls)
   1.147 +{    
   1.148 +    // Inject a SDL_QUIT event
   1.149 +    SDL_SendQuit();
   1.150 +}
   1.151 +
   1.152 +extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
   1.153 +                                    JNIEnv* env, jclass cls)
   1.154 +{
   1.155 +    /* This is the audio thread, with a different environment */
   1.156 +    mAudioEnv = env;
   1.157 +
   1.158 +    Android_RunAudioThread();
   1.159 +}
   1.160 +
   1.161 +
   1.162 +/*******************************************************************************
   1.163 +             Functions called by SDL into Java
   1.164 +*******************************************************************************/
   1.165 +extern "C" void Android_JNI_CreateContext()
   1.166 +{
   1.167 +    mEnv->CallStaticVoidMethod(mActivityClass, midCreateGLContext); 
   1.168 +}
   1.169 +
   1.170 +extern "C" void Android_JNI_SwapWindow()
   1.171 +{
   1.172 +    mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers); 
   1.173 +}
   1.174 +
   1.175 +extern "C" void Android_JNI_SetActivityTitle(const char *title)
   1.176 +{
   1.177 +    jmethodID mid;
   1.178 +
   1.179 +    mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
   1.180 +    if (mid) {
   1.181 +        mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title));
   1.182 +    }
   1.183 +}
   1.184 +
   1.185 +extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
   1.186 +{
   1.187 +    int i;
   1.188 +    for (i = 0; i < 3; ++i) {
   1.189 +        values[i] = fLastAccelerometer[i];
   1.190 +    }
   1.191 +}
   1.192 +
   1.193 +//
   1.194 +// Audio support
   1.195 +//
   1.196 +static jboolean audioBuffer16Bit = JNI_FALSE;
   1.197 +static jboolean audioBufferStereo = JNI_FALSE;
   1.198 +static jobject audioBuffer = NULL;
   1.199 +static void* audioBufferPinned = NULL;
   1.200 +
   1.201 +extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
   1.202 +{
   1.203 +    int audioBufferFrames;
   1.204 +
   1.205 +    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
   1.206 +    audioBuffer16Bit = is16Bit;
   1.207 +    audioBufferStereo = channelCount > 1;
   1.208 +
   1.209 +    audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
   1.210 +
   1.211 +    if (audioBuffer == NULL) {
   1.212 +        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
   1.213 +        return 0;
   1.214 +    }
   1.215 +    audioBuffer = mEnv->NewGlobalRef(audioBuffer);
   1.216 +
   1.217 +    jboolean isCopy = JNI_FALSE;
   1.218 +    if (audioBuffer16Bit) {
   1.219 +        audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
   1.220 +        audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
   1.221 +    } else {
   1.222 +        audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
   1.223 +        audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
   1.224 +    }
   1.225 +    if (audioBufferStereo) {
   1.226 +        audioBufferFrames /= 2;
   1.227 +    }
   1.228 +
   1.229 +    return audioBufferFrames;
   1.230 +}
   1.231 +
   1.232 +extern "C" void * Android_JNI_GetAudioBuffer()
   1.233 +{
   1.234 +    return audioBufferPinned;
   1.235 +}
   1.236 +
   1.237 +extern "C" void Android_JNI_WriteAudioBuffer()
   1.238 +{
   1.239 +    if (audioBuffer16Bit) {
   1.240 +        mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
   1.241 +        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
   1.242 +    } else {
   1.243 +        mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
   1.244 +        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
   1.245 +    }
   1.246 +
   1.247 +    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
   1.248 +}
   1.249 +
   1.250 +extern "C" void Android_JNI_CloseAudioDevice()
   1.251 +{
   1.252 +    mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit); 
   1.253 +
   1.254 +    if (audioBuffer) {
   1.255 +        mEnv->DeleteGlobalRef(audioBuffer);
   1.256 +        audioBuffer = NULL;
   1.257 +        audioBufferPinned = NULL;
   1.258 +    }
   1.259 +}
   1.260 +
   1.261 +/* vi: set ts=4 sw=4 expandtab: */