src/SDL_android.cpp
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Jan 2011 17:53:06 -0800
changeset 4989 58b6bb4a45e9
parent 4981 55b82067815b
child 4995 9f9bea41e88f
permissions -rw-r--r--
More Android cleanup:
* Formalized the interface with Java methods in SDL_android.h
* We don't need the feature system, at least right now
* Fixed waiting for the SDLMain thread
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@4980
    30
}
slouken@4980
    31
slouken@4964
    32
/*******************************************************************************
slouken@4964
    33
 This file links the Java side of Android with libsdl
slouken@4964
    34
*******************************************************************************/
slouken@4964
    35
#include <jni.h>
slouken@4964
    36
#include <android/log.h>
slouken@4964
    37
slouken@4964
    38
slouken@4964
    39
/*******************************************************************************
slouken@4964
    40
                               Globals
slouken@4964
    41
*******************************************************************************/
slouken@4964
    42
JavaVM* mVM = NULL;
slouken@4964
    43
JNIEnv* mEnv = NULL;
slouken@4964
    44
JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
slouken@4964
    45
slouken@4964
    46
//Main activity
slouken@4964
    47
jclass mActivityInstance;
slouken@4964
    48
slouken@4964
    49
//method signatures
slouken@4964
    50
jmethodID midCreateGLContext;
slouken@4964
    51
jmethodID midFlipBuffers;
slouken@4964
    52
jmethodID midUpdateAudio;
slouken@4964
    53
slouken@4964
    54
//Feature IDs
slouken@4964
    55
static const int FEATURE_AUDIO = 1;
slouken@4964
    56
static const int FEATURE_ACCEL = 2;
slouken@4964
    57
slouken@4964
    58
//Accelerometer data storage
slouken@4964
    59
float fLastAccelerometer[3];
slouken@4964
    60
slouken@4964
    61
slouken@4964
    62
/*******************************************************************************
slouken@4964
    63
                 Functions called by JNI
slouken@4964
    64
*******************************************************************************/
slouken@4964
    65
slouken@4964
    66
// Library init
slouken@4964
    67
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
slouken@4964
    68
{
slouken@4964
    69
    mVM = vm;
slouken@4964
    70
slouken@4964
    71
    return JNI_VERSION_1_4;
slouken@4964
    72
}
slouken@4964
    73
slouken@4964
    74
// Called before SDL_main() to initialize JNI bindings
slouken@4964
    75
extern "C" void SDL_Android_Init(JNIEnv* env)
slouken@4964
    76
{
slouken@4964
    77
    mEnv = env;
slouken@4964
    78
slouken@4964
    79
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
    80
slouken@4964
    81
    jclass cls = mEnv->FindClass ("org/libsdl/app/SDLActivity"); 
slouken@4964
    82
    mActivityInstance = cls;
slouken@4964
    83
    midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
slouken@4964
    84
    midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
slouken@4964
    85
    midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
slouken@4964
    86
slouken@4989
    87
    if(!midCreateGLContext || !midFlipBuffers || !midUpdateAudio) {
slouken@4964
    88
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
slouken@4964
    89
    } else {
slouken@4964
    90
#ifdef DEBUG
slouken@4964
    91
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
slouken@4964
    92
#endif
slouken@4964
    93
    }
slouken@4964
    94
}
slouken@4964
    95
slouken@4964
    96
// Keydown
slouken@4964
    97
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(JNIEnv* env, 
slouken@4964
    98
               jobject obj, jint keycode)
slouken@4964
    99
{
slouken@4964
   100
#ifdef DEBUG
slouken@4964
   101
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4980
   102
                        "SDL: native key down %d\n", keycode);
slouken@4964
   103
#endif
slouken@4980
   104
    Android_OnKeyDown(keycode);
slouken@4964
   105
}
slouken@4964
   106
slouken@4964
   107
// Keyup
slouken@4964
   108
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(JNIEnv* env, 
slouken@4964
   109
               jobject obj, jint keycode)
slouken@4964
   110
{
slouken@4964
   111
#ifdef DEBUG
slouken@4964
   112
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4980
   113
                        "SDL: native key up %d\n", keycode);
slouken@4964
   114
#endif
slouken@4980
   115
    Android_OnKeyUp(keycode);
slouken@4964
   116
}
slouken@4964
   117
slouken@4964
   118
// Touch
slouken@4964
   119
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(JNIEnv* env, 
slouken@4964
   120
               jobject obj, jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   121
{
slouken@4964
   122
#ifdef DEBUG
slouken@4964
   123
    __android_log_print(ANDROID_LOG_INFO, "SDL", 
slouken@4964
   124
                        "SDL: native touch event %d @ %f/%f, pressure %f\n", 
slouken@4964
   125
                        action, x, y, p);
slouken@4964
   126
#endif
slouken@4964
   127
slouken@4964
   128
    //TODO: Pass this off to the SDL multitouch stuff
slouken@4964
   129
}
slouken@4964
   130
slouken@4964
   131
// Quit
slouken@4964
   132
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv*  env, 
slouken@4964
   133
                                                                jobject obj )
slouken@4964
   134
{    
slouken@4964
   135
    // Inject a SDL_QUIT event
slouken@4964
   136
    SDL_SendQuit();
slouken@4964
   137
}
slouken@4964
   138
slouken@4964
   139
// Resize
slouken@4964
   140
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
slouken@4964
   141
                                        JNIEnv*  env, jobject obj, jint width, 
slouken@4964
   142
                                        jint height, jint format)
slouken@4964
   143
{
slouken@4981
   144
    Android_SetScreenResolution(width, height, format);
slouken@4964
   145
}
slouken@4964
   146
slouken@4964
   147
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
slouken@4964
   148
                                        JNIEnv*  env, jobject obj,
slouken@4964
   149
                                        jfloat x, jfloat y, jfloat z)
slouken@4964
   150
{
slouken@4964
   151
    fLastAccelerometer[0] = x;
slouken@4964
   152
    fLastAccelerometer[1] = y;
slouken@4964
   153
    fLastAccelerometer[2] = z;   
slouken@4964
   154
}
slouken@4964
   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@4989
   171
extern "C" void Android_JNI_UpdateAudioBuffer(unsigned char *buf, int len)
slouken@4964
   172
{
slouken@4964
   173
    //Annoyingly we can't just call into Java from any thread. Because the audio
slouken@4964
   174
    //callback is dispatched from the SDL audio thread (that wasn't made from
slouken@4964
   175
    //java, we have to do some magic here to let the JVM know about the thread.
slouken@4964
   176
    //Because everything it touches on the Java side is static anyway, it's 
slouken@4964
   177
    //not a big deal, just annoying.
slouken@4964
   178
    if(!mAudioThreadEnv) {
slouken@4964
   179
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
slouken@4964
   180
slouken@4964
   181
        mVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
slouken@4964
   182
slouken@4964
   183
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
slouken@4964
   184
    }
slouken@4964
   185
    
slouken@4964
   186
    jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
slouken@4964
   187
slouken@4964
   188
    //blah. We probably should rework this so we avoid the copy. 
slouken@4964
   189
    mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
slouken@4964
   190
    
slouken@4964
   191
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
slouken@4964
   192
slouken@4964
   193
    mAudioThreadEnv->CallStaticVoidMethod(  mActivityInstance, 
slouken@4964
   194
                                            midUpdateAudio, arr );
slouken@4964
   195
slouken@4964
   196
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
slouken@4964
   197
    
slouken@4964
   198
}
slouken@4981
   199
slouken@4981
   200
/* vi: set ts=4 sw=4 expandtab: */