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