src/SDL_android.cpp
author Sam Lantinga <slouken@libsdl.org>
Thu, 13 Jan 2011 11:14:20 -0800
changeset 4995 9f9bea41e88f
parent 4989 58b6bb4a45e9
child 4996 8d7315668e35
permissions -rw-r--r--
Working audio implementation contributed by Joseph Lunderville
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@4964
    58
slouken@4995
    59
// Accelerometer data storage
slouken@4964
    60
float fLastAccelerometer[3];
slouken@4964
    61
slouken@4964
    62
slouken@4964
    63
/*******************************************************************************
slouken@4964
    64
                 Functions called by JNI
slouken@4964
    65
*******************************************************************************/
slouken@4964
    66
slouken@4964
    67
// Library init
slouken@4964
    68
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
slouken@4964
    69
{
slouken@4964
    70
    mVM = vm;
slouken@4964
    71
slouken@4964
    72
    return JNI_VERSION_1_4;
slouken@4964
    73
}
slouken@4964
    74
slouken@4964
    75
// Called before SDL_main() to initialize JNI bindings
slouken@4964
    76
extern "C" void SDL_Android_Init(JNIEnv* env)
slouken@4964
    77
{
slouken@4964
    78
    mEnv = env;
slouken@4964
    79
slouken@4964
    80
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
    81
slouken@4964
    82
    jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity"); 
slouken@4964
    83
    mActivityInstance = cls;
slouken@4964
    84
    midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
slouken@4964
    85
    midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
slouken@4995
    86
	midAudioInit = mEnv->GetStaticMethodID(cls, "audioInit", "(IZZI)Ljava/lang/Object;");
slouken@4995
    87
	midAudioWriteShortBuffer = mEnv->GetStaticMethodID(cls, "audioWriteShortBuffer", "([S)V");
slouken@4995
    88
	midAudioWriteByteBuffer = mEnv->GetStaticMethodID(cls, "audioWriteByteBuffer", "([B)V");
slouken@4964
    89
slouken@4995
    90
    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
slouken@4995
    91
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer) {
slouken@4995
    92
		__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
slouken@4964
    93
    }
slouken@4964
    94
}
slouken@4964
    95
slouken@4995
    96
// Resize
slouken@4995
    97
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
slouken@4995
    98
                                    JNIEnv* env, jobject obj,
slouken@4995
    99
                                    jint width, jint height, jint format)
slouken@4995
   100
{
slouken@4995
   101
    Android_SetScreenResolution(width, height, format);
slouken@4995
   102
}
slouken@4995
   103
slouken@4964
   104
// Keydown
slouken@4995
   105
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
slouken@4995
   106
                                    JNIEnv* env, jobject obj, jint keycode)
slouken@4964
   107
{
slouken@4980
   108
    Android_OnKeyDown(keycode);
slouken@4964
   109
}
slouken@4964
   110
slouken@4964
   111
// Keyup
slouken@4995
   112
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
slouken@4995
   113
                                    JNIEnv* env, jobject obj, jint keycode)
slouken@4964
   114
{
slouken@4980
   115
    Android_OnKeyUp(keycode);
slouken@4964
   116
}
slouken@4964
   117
slouken@4964
   118
// Touch
slouken@4995
   119
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
slouken@4995
   120
                                    JNIEnv* env, jobject obj,
slouken@4995
   121
                                    jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   122
{
slouken@4964
   123
#ifdef DEBUG
slouken@4964
   124
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4964
   125
                        "SDL: native touch event %d @ %f/%f, pressure %f\n", 
slouken@4964
   126
                        action, x, y, p);
slouken@4964
   127
#endif
slouken@4964
   128
slouken@4964
   129
    //TODO: Pass this off to the SDL multitouch stuff
slouken@4964
   130
}
slouken@4964
   131
slouken@4995
   132
// Accelerometer
slouken@4964
   133
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
slouken@4995
   134
                                    JNIEnv* env, jobject obj,
slouken@4995
   135
                                    jfloat x, jfloat y, jfloat z)
slouken@4964
   136
{
slouken@4964
   137
    fLastAccelerometer[0] = x;
slouken@4964
   138
    fLastAccelerometer[1] = y;
slouken@4964
   139
    fLastAccelerometer[2] = z;   
slouken@4964
   140
}
slouken@4964
   141
slouken@4995
   142
// Quit
slouken@4995
   143
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
slouken@4995
   144
                                    JNIEnv* env, jobject obj)
slouken@4995
   145
{    
slouken@4995
   146
    // Inject a SDL_QUIT event
slouken@4995
   147
    SDL_SendQuit();
slouken@4995
   148
}
slouken@4995
   149
slouken@4995
   150
extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
slouken@4995
   151
                                    JNIEnv* env)
slouken@4995
   152
{
slouken@4995
   153
	mVM->AttachCurrentThread(&mAudioEnv, NULL);
slouken@4995
   154
	Android_RunAudioThread();
slouken@4995
   155
}
slouken@4964
   156
slouken@4964
   157
slouken@4964
   158
/*******************************************************************************
slouken@4964
   159
             Functions called by SDL into Java
slouken@4964
   160
*******************************************************************************/
slouken@4989
   161
extern "C" void Android_JNI_CreateContext()
slouken@4964
   162
{
slouken@4989
   163
    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext); 
slouken@4964
   164
}
slouken@4964
   165
slouken@4989
   166
extern "C" void Android_JNI_SwapWindow()
slouken@4964
   167
{
slouken@4964
   168
    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers); 
slouken@4964
   169
}
slouken@4964
   170
slouken@4995
   171
//
slouken@4995
   172
// Audio support
slouken@4995
   173
//
slouken@4995
   174
static jint audioBufferFrames = 0;
slouken@4995
   175
static bool audioBuffer16Bit = false;
slouken@4995
   176
static bool audioBufferStereo = false;
slouken@4995
   177
slouken@4995
   178
static jobject audioBuffer;
slouken@4995
   179
static void * audioPinnedBuffer;
slouken@4995
   180
slouken@4995
   181
extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
slouken@4964
   182
{
slouken@4995
   183
	__android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
slouken@4995
   184
	audioBuffer16Bit = is16Bit;
slouken@4995
   185
	audioBufferStereo = channelCount > 1;
slouken@4964
   186
slouken@4995
   187
	audioBuffer = mEnv->CallStaticObjectMethod(mActivityInstance, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
slouken@4995
   188
	audioBuffer = mEnv->NewGlobalRef(audioBuffer);
slouken@4964
   189
slouken@4995
   190
	if (audioBuffer == NULL) {
slouken@4995
   191
		__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
slouken@4995
   192
		return 0;
slouken@4995
   193
	}
slouken@4995
   194
slouken@4995
   195
	if (audioBufferStereo) {
slouken@4995
   196
		audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer) / 2;
slouken@4995
   197
	} else {
slouken@4995
   198
		audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
slouken@4995
   199
	}
slouken@4995
   200
slouken@4995
   201
	return audioBufferFrames;
slouken@4995
   202
}
slouken@4995
   203
slouken@4995
   204
extern "C" void * Android_JNI_PinAudioBuffer()
slouken@4995
   205
{
slouken@4995
   206
	jboolean isCopy = JNI_FALSE;
slouken@4995
   207
slouken@4995
   208
	if (audioPinnedBuffer != NULL) {
slouken@4995
   209
		return audioPinnedBuffer;
slouken@4995
   210
	}
slouken@4995
   211
slouken@4995
   212
	if (audioBuffer16Bit) {
slouken@4995
   213
		audioPinnedBuffer = mAudioEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
slouken@4995
   214
	} else {
slouken@4995
   215
		audioPinnedBuffer = mAudioEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
slouken@4995
   216
	}
slouken@4995
   217
slouken@4995
   218
	return audioPinnedBuffer;
slouken@4995
   219
}
slouken@4995
   220
slouken@4995
   221
extern "C" void Android_JNI_WriteAudioBufferAndUnpin()
slouken@4995
   222
{
slouken@4995
   223
	if (audioPinnedBuffer == NULL) {
slouken@4995
   224
		return;
slouken@4995
   225
	}
slouken@4995
   226
slouken@4995
   227
	if (audioBuffer16Bit) {
slouken@4995
   228
		mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioPinnedBuffer, JNI_COMMIT);
slouken@4995
   229
		mAudioEnv->CallStaticVoidMethod(mActivityInstance, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
slouken@4995
   230
	} else {
slouken@4995
   231
		mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioPinnedBuffer, JNI_COMMIT);
slouken@4995
   232
		mAudioEnv->CallStaticVoidMethod(mActivityInstance, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
slouken@4995
   233
	}
slouken@4995
   234
slouken@4995
   235
	audioPinnedBuffer = NULL;
slouken@4995
   236
}
slouken@4995
   237
slouken@4995
   238
extern "C" void Android_JNI_CloseAudioDevice()
slouken@4995
   239
{
slouken@4995
   240
    if (audioBuffer) {
slouken@4995
   241
        mEnv->DeleteGlobalRef(audioBuffer);
slouken@4995
   242
        audioBuffer = NULL;
slouken@4964
   243
    }
slouken@4964
   244
slouken@4995
   245
	// TODO: Implement
slouken@4964
   246
}
slouken@4981
   247
slouken@4981
   248
/* vi: set ts=4 sw=4 expandtab: */