src/SDL_android.cpp
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Jan 2011 13:52:41 -0800
changeset 4980 d9fdff945ec9
parent 4964 6c645018741e
child 4981 55b82067815b
permissions -rw-r--r--
A bit of cleanup in the Android driver
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@4980
    24
extern "C" {
slouken@4980
    25
#include "events/SDL_events_c.h"
slouken@4980
    26
#include "video/android/SDL_androidkeyboard.h"
slouken@4980
    27
}
slouken@4980
    28
slouken@4964
    29
/*******************************************************************************
slouken@4964
    30
 This file links the Java side of Android with libsdl
slouken@4964
    31
*******************************************************************************/
slouken@4964
    32
#include <jni.h>
slouken@4964
    33
#include <android/log.h>
slouken@4964
    34
slouken@4964
    35
slouken@4964
    36
/*******************************************************************************
slouken@4964
    37
                               Globals
slouken@4964
    38
*******************************************************************************/
slouken@4964
    39
JavaVM* mVM = NULL;
slouken@4964
    40
JNIEnv* mEnv = NULL;
slouken@4964
    41
JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
slouken@4964
    42
slouken@4964
    43
//Main activity
slouken@4964
    44
jclass mActivityInstance;
slouken@4964
    45
slouken@4964
    46
//method signatures
slouken@4964
    47
jmethodID midCreateGLContext;
slouken@4964
    48
jmethodID midFlipBuffers;
slouken@4964
    49
jmethodID midEnableFeature;
slouken@4964
    50
jmethodID midUpdateAudio;
slouken@4964
    51
slouken@4964
    52
extern "C" void Android_SetScreenResolution(int width, int height);
slouken@4964
    53
extern "C" int SDL_SendQuit();
slouken@4964
    54
slouken@4964
    55
//If we're not the active app, don't try to render
slouken@4964
    56
bool bRenderingEnabled = false;
slouken@4964
    57
slouken@4964
    58
//Feature IDs
slouken@4964
    59
static const int FEATURE_AUDIO = 1;
slouken@4964
    60
static const int FEATURE_ACCEL = 2;
slouken@4964
    61
slouken@4964
    62
//Accelerometer data storage
slouken@4964
    63
float fLastAccelerometer[3];
slouken@4964
    64
slouken@4964
    65
slouken@4964
    66
/*******************************************************************************
slouken@4964
    67
                 Functions called by JNI
slouken@4964
    68
*******************************************************************************/
slouken@4964
    69
slouken@4964
    70
// Library init
slouken@4964
    71
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
slouken@4964
    72
{
slouken@4964
    73
    mVM = vm;
slouken@4964
    74
slouken@4964
    75
    return JNI_VERSION_1_4;
slouken@4964
    76
}
slouken@4964
    77
slouken@4964
    78
// Called before SDL_main() to initialize JNI bindings
slouken@4964
    79
extern "C" void SDL_Android_Init(JNIEnv* env)
slouken@4964
    80
{
slouken@4964
    81
    mEnv = env;
slouken@4964
    82
slouken@4964
    83
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
    84
slouken@4964
    85
    jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity"); 
slouken@4964
    86
    mActivityInstance = cls;
slouken@4964
    87
    midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
slouken@4964
    88
    midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
slouken@4964
    89
    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
slouken@4964
    90
    midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
slouken@4964
    91
slouken@4964
    92
    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
slouken@4964
    93
        !midUpdateAudio) {
slouken@4964
    94
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
slouken@4964
    95
    } else {
slouken@4964
    96
#ifdef DEBUG
slouken@4964
    97
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
slouken@4964
    98
#endif
slouken@4964
    99
    }
slouken@4964
   100
}
slouken@4964
   101
slouken@4964
   102
// Keydown
slouken@4964
   103
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(JNIEnv* env, 
slouken@4964
   104
               jobject obj, jint keycode)
slouken@4964
   105
{
slouken@4964
   106
#ifdef DEBUG
slouken@4964
   107
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4980
   108
                        "SDL: native key down %d\n", keycode);
slouken@4964
   109
#endif
slouken@4980
   110
    Android_OnKeyDown(keycode);
slouken@4964
   111
}
slouken@4964
   112
slouken@4964
   113
// Keyup
slouken@4964
   114
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(JNIEnv* env, 
slouken@4964
   115
               jobject obj, jint keycode)
slouken@4964
   116
{
slouken@4964
   117
#ifdef DEBUG
slouken@4964
   118
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4980
   119
                        "SDL: native key up %d\n", keycode);
slouken@4964
   120
#endif
slouken@4980
   121
    Android_OnKeyUp(keycode);
slouken@4964
   122
}
slouken@4964
   123
slouken@4964
   124
// Touch
slouken@4964
   125
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(JNIEnv* env, 
slouken@4964
   126
               jobject obj, jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   127
{
slouken@4964
   128
#ifdef DEBUG
slouken@4964
   129
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4964
   130
                        "SDL: native touch event %d @ %f/%f, pressure %f\n", 
slouken@4964
   131
                        action, x, y, p);
slouken@4964
   132
#endif
slouken@4964
   133
slouken@4964
   134
    //TODO: Pass this off to the SDL multitouch stuff
slouken@4964
   135
}
slouken@4964
   136
slouken@4964
   137
// Quit
slouken@4964
   138
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv*  env, 
slouken@4964
   139
                                                                jobject obj )
slouken@4964
   140
{    
slouken@4964
   141
    // Stop rendering as we're no longer in the foreground
slouken@4964
   142
    bRenderingEnabled = false;
slouken@4964
   143
slouken@4964
   144
    // Inject a SDL_QUIT event
slouken@4964
   145
    SDL_SendQuit();
slouken@4964
   146
}
slouken@4964
   147
slouken@4964
   148
// Screen size
slouken@4964
   149
extern "C" void Java_org_libsdl_app_SDLActivity_nativeSetScreenSize(
slouken@4964
   150
                JNIEnv*  env, jobject obj, jint width, jint height)
slouken@4964
   151
{
slouken@4964
   152
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4964
   153
                        "SDL: Set screen size on init: %d/%d\n", width, height);
slouken@4964
   154
    Android_SetScreenResolution(width, height);
slouken@4964
   155
}
slouken@4964
   156
slouken@4964
   157
// Resize
slouken@4964
   158
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
slouken@4964
   159
                                        JNIEnv*  env, jobject obj, jint width, 
slouken@4964
   160
                                        jint height, jint format)
slouken@4964
   161
{
slouken@4980
   162
    /* FIXME: What is the relationship between this and the window? */
slouken@4964
   163
}
slouken@4964
   164
slouken@4964
   165
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
slouken@4964
   166
                                        JNIEnv*  env, jobject obj,
slouken@4964
   167
                                        jfloat x, jfloat y, jfloat z)
slouken@4964
   168
{
slouken@4964
   169
    fLastAccelerometer[0] = x;
slouken@4964
   170
    fLastAccelerometer[1] = y;
slouken@4964
   171
    fLastAccelerometer[2] = z;   
slouken@4964
   172
}
slouken@4964
   173
slouken@4964
   174
slouken@4964
   175
slouken@4964
   176
/*******************************************************************************
slouken@4964
   177
             Functions called by SDL into Java
slouken@4964
   178
*******************************************************************************/
slouken@4964
   179
extern "C" void Android_CreateContext()
slouken@4964
   180
{
slouken@4964
   181
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
slouken@4964
   182
slouken@4964
   183
    bRenderingEnabled = true;
slouken@4964
   184
slouken@4964
   185
    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
slouken@4964
   186
}
slouken@4964
   187
slouken@4964
   188
extern "C" void Android_Render()
slouken@4964
   189
{
slouken@4964
   190
    if (!bRenderingEnabled) {
slouken@4964
   191
        return;
slouken@4964
   192
    }
slouken@4964
   193
slouken@4964
   194
    // When we get here, we've accumulated a full frame    
slouken@4964
   195
    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers); 
slouken@4964
   196
}
slouken@4964
   197
slouken@4964
   198
extern "C" void Android_EnableFeature(int featureid, bool enabled)
slouken@4964
   199
{
slouken@4964
   200
    mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature, 
slouken@4964
   201
                                featureid, (int)enabled); 
slouken@4964
   202
}
slouken@4964
   203
slouken@4964
   204
extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len)
slouken@4964
   205
{
slouken@4964
   206
    //Annoyingly we can't just call into Java from any thread. Because the audio
slouken@4964
   207
    //callback is dispatched from the SDL audio thread (that wasn't made from
slouken@4964
   208
    //java, we have to do some magic here to let the JVM know about the thread.
slouken@4964
   209
    //Because everything it touches on the Java side is static anyway, it's 
slouken@4964
   210
    //not a big deal, just annoying.
slouken@4964
   211
    if(!mAudioThreadEnv) {
slouken@4964
   212
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
slouken@4964
   213
slouken@4964
   214
        mVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
slouken@4964
   215
slouken@4964
   216
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
slouken@4964
   217
    }
slouken@4964
   218
    
slouken@4964
   219
    jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
slouken@4964
   220
slouken@4964
   221
    //blah. We probably should rework this so we avoid the copy. 
slouken@4964
   222
    mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
slouken@4964
   223
    
slouken@4964
   224
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
slouken@4964
   225
slouken@4964
   226
    mAudioThreadEnv->CallStaticVoidMethod(  mActivityInstance, 
slouken@4964
   227
                                            midUpdateAudio, arr );
slouken@4964
   228
slouken@4964
   229
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
slouken@4964
   230
    
slouken@4964
   231
}