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