src/SDL_android.cpp
author Sam Lantinga <slouken@libsdl.org>
Thu, 13 Jan 2011 15:10:17 -0800
changeset 4998 a514bfe6952a
parent 4997 a21501393bef
child 5000 6a10693e66c3
permissions -rw-r--r--
The window is changed to reflect the actual screen dimensions, for now.
Implemented SDL_SetWindowTitle(), which turned out to be fairly complex
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 JNIEnv* mEnv = NULL;
slouken@4995
    46
static JNIEnv* mAudioEnv = NULL;
slouken@4964
    47
slouken@4995
    48
// Main activity
slouken@4998
    49
static jclass mActivityClass;
slouken@4964
    50
slouken@4995
    51
// method signatures
slouken@4995
    52
static jmethodID midCreateGLContext;
slouken@4995
    53
static jmethodID midFlipBuffers;
slouken@4995
    54
static jmethodID midAudioInit;
slouken@4995
    55
static jmethodID midAudioWriteShortBuffer;
slouken@4995
    56
static jmethodID midAudioWriteByteBuffer;
slouken@4996
    57
static jmethodID midAudioQuit;
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
    return JNI_VERSION_1_4;
slouken@4964
    71
}
slouken@4964
    72
slouken@4964
    73
// Called before SDL_main() to initialize JNI bindings
slouken@4998
    74
extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
slouken@4964
    75
{
slouken@4964
    76
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
    77
slouken@4998
    78
    mEnv = env;
slouken@4998
    79
    mActivityClass = cls;
slouken@4998
    80
slouken@4998
    81
    midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    82
                                "createGLContext","()V");
slouken@4998
    83
    midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    84
                                "flipBuffers","()V");
slouken@4998
    85
    midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
slouken@4998
    86
                                "audioInit", "(IZZI)Ljava/lang/Object;");
slouken@4998
    87
    midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    88
                                "audioWriteShortBuffer", "([S)V");
slouken@4998
    89
    midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    90
                                "audioWriteByteBuffer", "([B)V");
slouken@4998
    91
    midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    92
                                "audioQuit", "()V");
slouken@4964
    93
slouken@4995
    94
    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
slouken@4996
    95
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
slouken@4996
    96
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
slouken@4964
    97
    }
slouken@4964
    98
}
slouken@4964
    99
slouken@4995
   100
// Resize
slouken@4995
   101
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
slouken@4998
   102
                                    JNIEnv* env, jclass jcls,
slouken@4995
   103
                                    jint width, jint height, jint format)
slouken@4995
   104
{
slouken@4995
   105
    Android_SetScreenResolution(width, height, format);
slouken@4995
   106
}
slouken@4995
   107
slouken@4964
   108
// Keydown
slouken@4995
   109
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
slouken@4998
   110
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   111
{
slouken@4980
   112
    Android_OnKeyDown(keycode);
slouken@4964
   113
}
slouken@4964
   114
slouken@4964
   115
// Keyup
slouken@4995
   116
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
slouken@4998
   117
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   118
{
slouken@4980
   119
    Android_OnKeyUp(keycode);
slouken@4964
   120
}
slouken@4964
   121
slouken@4964
   122
// Touch
slouken@4995
   123
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
slouken@4998
   124
                                    JNIEnv* env, jclass jcls,
slouken@4995
   125
                                    jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   126
{
slouken@4964
   127
#ifdef DEBUG
slouken@4964
   128
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4964
   129
                        "SDL: native touch event %d @ %f/%f, pressure %f\n", 
slouken@4964
   130
                        action, x, y, p);
slouken@4964
   131
#endif
slouken@4964
   132
slouken@4964
   133
    //TODO: Pass this off to the SDL multitouch stuff
slouken@4964
   134
}
slouken@4964
   135
slouken@4995
   136
// Accelerometer
slouken@4964
   137
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
slouken@4998
   138
                                    JNIEnv* env, jclass jcls,
slouken@4995
   139
                                    jfloat x, jfloat y, jfloat z)
slouken@4964
   140
{
slouken@4964
   141
    fLastAccelerometer[0] = x;
slouken@4964
   142
    fLastAccelerometer[1] = y;
slouken@4964
   143
    fLastAccelerometer[2] = z;   
slouken@4964
   144
}
slouken@4964
   145
slouken@4995
   146
// Quit
slouken@4995
   147
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
slouken@4998
   148
                                    JNIEnv* env, jclass cls)
slouken@4995
   149
{    
slouken@4995
   150
    // Inject a SDL_QUIT event
slouken@4995
   151
    SDL_SendQuit();
slouken@4995
   152
}
slouken@4995
   153
slouken@4995
   154
extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
slouken@4998
   155
                                    JNIEnv* env, jclass cls)
slouken@4995
   156
{
slouken@4998
   157
    /* This is the audio thread, with a different environment */
slouken@4998
   158
    mAudioEnv = env;
slouken@4998
   159
slouken@4996
   160
    Android_RunAudioThread();
slouken@4995
   161
}
slouken@4964
   162
slouken@4964
   163
slouken@4964
   164
/*******************************************************************************
slouken@4964
   165
             Functions called by SDL into Java
slouken@4964
   166
*******************************************************************************/
slouken@4989
   167
extern "C" void Android_JNI_CreateContext()
slouken@4964
   168
{
slouken@4998
   169
    mEnv->CallStaticVoidMethod(mActivityClass, midCreateGLContext); 
slouken@4964
   170
}
slouken@4964
   171
slouken@4989
   172
extern "C" void Android_JNI_SwapWindow()
slouken@4964
   173
{
slouken@4998
   174
    mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers); 
slouken@4998
   175
}
slouken@4998
   176
slouken@4998
   177
extern "C" void Android_JNI_SetActivityTitle(const char *title)
slouken@4998
   178
{
slouken@4998
   179
    jmethodID mid;
slouken@4998
   180
slouken@4998
   181
    mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
slouken@4998
   182
    if (mid) {
slouken@4998
   183
        mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title));
slouken@4998
   184
    }
slouken@4964
   185
}
slouken@4964
   186
slouken@4995
   187
//
slouken@4995
   188
// Audio support
slouken@4995
   189
//
slouken@4996
   190
static jboolean audioBuffer16Bit = JNI_FALSE;
slouken@4996
   191
static jboolean audioBufferStereo = JNI_FALSE;
slouken@4996
   192
static jobject audioBuffer = NULL;
slouken@4996
   193
static void* audioBufferPinned = NULL;
slouken@4995
   194
slouken@4995
   195
extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
slouken@4964
   196
{
slouken@4996
   197
    int audioBufferFrames;
slouken@4964
   198
slouken@4996
   199
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
slouken@4996
   200
    audioBuffer16Bit = is16Bit;
slouken@4996
   201
    audioBufferStereo = channelCount > 1;
slouken@4964
   202
slouken@4998
   203
    audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
slouken@4995
   204
slouken@4996
   205
    if (audioBuffer == NULL) {
slouken@4996
   206
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
slouken@4996
   207
        return 0;
slouken@4996
   208
    }
slouken@4996
   209
    audioBuffer = mEnv->NewGlobalRef(audioBuffer);
slouken@4995
   210
slouken@4996
   211
    jboolean isCopy = JNI_FALSE;
slouken@4996
   212
    if (audioBuffer16Bit) {
slouken@4996
   213
        audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
slouken@4996
   214
        audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
slouken@4996
   215
    } else {
slouken@4996
   216
        audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
slouken@4996
   217
        audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
slouken@4996
   218
    }
slouken@4996
   219
    if (audioBufferStereo) {
slouken@4996
   220
        audioBufferFrames /= 2;
slouken@4996
   221
    }
slouken@4996
   222
slouken@4996
   223
    return audioBufferFrames;
slouken@4995
   224
}
slouken@4995
   225
slouken@4996
   226
extern "C" void * Android_JNI_GetAudioBuffer()
slouken@4995
   227
{
slouken@4996
   228
    return audioBufferPinned;
slouken@4995
   229
}
slouken@4995
   230
slouken@4996
   231
extern "C" void Android_JNI_WriteAudioBuffer()
slouken@4995
   232
{
slouken@4996
   233
    if (audioBuffer16Bit) {
slouken@4996
   234
        mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
slouken@4998
   235
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
slouken@4996
   236
    } else {
slouken@4996
   237
        mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
slouken@4998
   238
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
slouken@4996
   239
    }
slouken@4995
   240
slouken@4996
   241
    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
slouken@4995
   242
}
slouken@4995
   243
slouken@4995
   244
extern "C" void Android_JNI_CloseAudioDevice()
slouken@4995
   245
{
slouken@4998
   246
    mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit); 
slouken@4964
   247
slouken@4997
   248
    if (audioBuffer) {
slouken@4997
   249
        mEnv->DeleteGlobalRef(audioBuffer);
slouken@4997
   250
        audioBuffer = NULL;
slouken@4997
   251
        audioBufferPinned = NULL;
slouken@4997
   252
    }
slouken@4964
   253
}
slouken@4981
   254
slouken@4981
   255
/* vi: set ts=4 sw=4 expandtab: */