src/core/android/SDL_android.cpp
author Tim Angus <tim@blackcompanystudios.co.uk>
Fri, 14 Oct 2011 17:29:49 +0100
changeset 5993 c9cb52d6d864
parent 5982 f324bd81b52c
child 5994 153d15ab3032
permissions -rw-r--r--
* Android's InputStream::skip is apparently buggy, so instead read into a dummy buffer
slouken@4964
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@4964
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@4964
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@4964
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@4964
    20
*/
slouken@4964
    21
#include "SDL_config.h"
slouken@5222
    22
#include "SDL_stdinc.h"
slouken@4964
    23
slouken@4989
    24
#include "SDL_android.h"
slouken@4989
    25
tim@5993
    26
#include <algorithm>
tim@5993
    27
slouken@4980
    28
extern "C" {
slouken@5092
    29
#include "../../events/SDL_events_c.h"
slouken@5092
    30
#include "../../video/android/SDL_androidkeyboard.h"
slouken@5092
    31
#include "../../video/android/SDL_androidtouch.h"
slouken@5092
    32
#include "../../video/android/SDL_androidvideo.h"
slouken@4995
    33
slouken@4995
    34
/* Impelemented in audio/android/SDL_androidaudio.c */
slouken@4995
    35
extern void Android_RunAudioThread();
slouken@4995
    36
} // C
slouken@4980
    37
slouken@4964
    38
/*******************************************************************************
slouken@4964
    39
 This file links the Java side of Android with libsdl
slouken@4964
    40
*******************************************************************************/
slouken@4964
    41
#include <jni.h>
slouken@4964
    42
#include <android/log.h>
slouken@4964
    43
slouken@4964
    44
slouken@4964
    45
/*******************************************************************************
slouken@4964
    46
                               Globals
slouken@4964
    47
*******************************************************************************/
slouken@4995
    48
static JNIEnv* mEnv = NULL;
slouken@4995
    49
static JNIEnv* mAudioEnv = NULL;
slouken@4964
    50
slouken@4995
    51
// Main activity
slouken@4998
    52
static jclass mActivityClass;
slouken@4964
    53
slouken@4995
    54
// method signatures
slouken@4995
    55
static jmethodID midCreateGLContext;
slouken@4995
    56
static jmethodID midFlipBuffers;
slouken@4995
    57
static jmethodID midAudioInit;
slouken@4995
    58
static jmethodID midAudioWriteShortBuffer;
slouken@4995
    59
static jmethodID midAudioWriteByteBuffer;
slouken@4996
    60
static jmethodID midAudioQuit;
slouken@4964
    61
slouken@4995
    62
// Accelerometer data storage
slouken@5000
    63
static 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
    return JNI_VERSION_1_4;
slouken@4964
    74
}
slouken@4964
    75
slouken@4964
    76
// Called before SDL_main() to initialize JNI bindings
slouken@4998
    77
extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
slouken@4964
    78
{
slouken@4964
    79
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
    80
slouken@4998
    81
    mEnv = env;
slouken@4998
    82
    mActivityClass = cls;
slouken@4998
    83
slouken@4998
    84
    midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
slouken@5222
    85
                                "createGLContext","(II)Z");
slouken@4998
    86
    midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    87
                                "flipBuffers","()V");
slouken@4998
    88
    midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
slouken@4998
    89
                                "audioInit", "(IZZI)Ljava/lang/Object;");
slouken@4998
    90
    midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    91
                                "audioWriteShortBuffer", "([S)V");
slouken@4998
    92
    midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    93
                                "audioWriteByteBuffer", "([B)V");
slouken@4998
    94
    midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    95
                                "audioQuit", "()V");
slouken@4964
    96
slouken@4995
    97
    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
slouken@4996
    98
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
slouken@4996
    99
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
slouken@4964
   100
    }
slouken@4964
   101
}
slouken@4964
   102
slouken@4995
   103
// Resize
slouken@4995
   104
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
slouken@4998
   105
                                    JNIEnv* env, jclass jcls,
slouken@4995
   106
                                    jint width, jint height, jint format)
slouken@4995
   107
{
slouken@4995
   108
    Android_SetScreenResolution(width, height, format);
slouken@4995
   109
}
slouken@4995
   110
slouken@4964
   111
// Keydown
slouken@4995
   112
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
slouken@4998
   113
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   114
{
slouken@4980
   115
    Android_OnKeyDown(keycode);
slouken@4964
   116
}
slouken@4964
   117
slouken@4964
   118
// Keyup
slouken@4995
   119
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
slouken@4998
   120
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   121
{
slouken@4980
   122
    Android_OnKeyUp(keycode);
slouken@4964
   123
}
slouken@4964
   124
slouken@4964
   125
// Touch
slouken@4995
   126
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
slouken@4998
   127
                                    JNIEnv* env, jclass jcls,
icculus@5982
   128
                                    jint touch_device_id_in, jint pointer_finger_id_in,
slouken@4995
   129
                                    jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   130
{
icculus@5982
   131
    Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p);
slouken@4964
   132
}
slouken@4964
   133
slouken@4995
   134
// Accelerometer
slouken@4964
   135
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
slouken@4998
   136
                                    JNIEnv* env, jclass jcls,
slouken@4995
   137
                                    jfloat x, jfloat y, jfloat z)
slouken@4964
   138
{
slouken@4964
   139
    fLastAccelerometer[0] = x;
slouken@4964
   140
    fLastAccelerometer[1] = y;
slouken@4964
   141
    fLastAccelerometer[2] = z;   
slouken@4964
   142
}
slouken@4964
   143
slouken@4995
   144
// Quit
slouken@4995
   145
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
slouken@4998
   146
                                    JNIEnv* env, jclass cls)
slouken@4995
   147
{    
slouken@4995
   148
    // Inject a SDL_QUIT event
slouken@4995
   149
    SDL_SendQuit();
slouken@4995
   150
}
slouken@4995
   151
slouken@4995
   152
extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
slouken@4998
   153
                                    JNIEnv* env, jclass cls)
slouken@4995
   154
{
slouken@4998
   155
    /* This is the audio thread, with a different environment */
slouken@4998
   156
    mAudioEnv = env;
slouken@4998
   157
slouken@4996
   158
    Android_RunAudioThread();
slouken@4995
   159
}
slouken@4964
   160
slouken@4964
   161
slouken@4964
   162
/*******************************************************************************
slouken@4964
   163
             Functions called by SDL into Java
slouken@4964
   164
*******************************************************************************/
slouken@5222
   165
extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
slouken@4964
   166
{
slouken@5222
   167
    if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
slouken@5222
   168
        return SDL_TRUE;
slouken@5222
   169
    } else {
slouken@5222
   170
        return SDL_FALSE;
slouken@5222
   171
    }
slouken@4964
   172
}
slouken@4964
   173
slouken@4989
   174
extern "C" void Android_JNI_SwapWindow()
slouken@4964
   175
{
slouken@4998
   176
    mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers); 
slouken@4998
   177
}
slouken@4998
   178
slouken@4998
   179
extern "C" void Android_JNI_SetActivityTitle(const char *title)
slouken@4998
   180
{
slouken@4998
   181
    jmethodID mid;
slouken@4998
   182
slouken@4998
   183
    mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
slouken@4998
   184
    if (mid) {
slouken@4998
   185
        mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title));
slouken@4998
   186
    }
slouken@4964
   187
}
slouken@4964
   188
slouken@5000
   189
extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
slouken@5000
   190
{
slouken@5000
   191
    int i;
slouken@5000
   192
    for (i = 0; i < 3; ++i) {
slouken@5000
   193
        values[i] = fLastAccelerometer[i];
slouken@5000
   194
    }
slouken@5000
   195
}
slouken@5000
   196
slouken@4995
   197
//
slouken@4995
   198
// Audio support
slouken@4995
   199
//
slouken@4996
   200
static jboolean audioBuffer16Bit = JNI_FALSE;
slouken@4996
   201
static jboolean audioBufferStereo = JNI_FALSE;
slouken@4996
   202
static jobject audioBuffer = NULL;
slouken@4996
   203
static void* audioBufferPinned = NULL;
slouken@4995
   204
slouken@4995
   205
extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
slouken@4964
   206
{
slouken@4996
   207
    int audioBufferFrames;
slouken@4964
   208
slouken@4996
   209
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
slouken@4996
   210
    audioBuffer16Bit = is16Bit;
slouken@4996
   211
    audioBufferStereo = channelCount > 1;
slouken@4964
   212
slouken@4998
   213
    audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
slouken@4995
   214
slouken@4996
   215
    if (audioBuffer == NULL) {
slouken@4996
   216
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
slouken@4996
   217
        return 0;
slouken@4996
   218
    }
slouken@4996
   219
    audioBuffer = mEnv->NewGlobalRef(audioBuffer);
slouken@4995
   220
slouken@4996
   221
    jboolean isCopy = JNI_FALSE;
slouken@4996
   222
    if (audioBuffer16Bit) {
slouken@4996
   223
        audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
slouken@4996
   224
        audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
slouken@4996
   225
    } else {
slouken@4996
   226
        audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
slouken@4996
   227
        audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
slouken@4996
   228
    }
slouken@4996
   229
    if (audioBufferStereo) {
slouken@4996
   230
        audioBufferFrames /= 2;
slouken@4996
   231
    }
slouken@4996
   232
slouken@4996
   233
    return audioBufferFrames;
slouken@4995
   234
}
slouken@4995
   235
slouken@4996
   236
extern "C" void * Android_JNI_GetAudioBuffer()
slouken@4995
   237
{
slouken@4996
   238
    return audioBufferPinned;
slouken@4995
   239
}
slouken@4995
   240
slouken@4996
   241
extern "C" void Android_JNI_WriteAudioBuffer()
slouken@4995
   242
{
slouken@4996
   243
    if (audioBuffer16Bit) {
slouken@4996
   244
        mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
slouken@4998
   245
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
slouken@4996
   246
    } else {
slouken@4996
   247
        mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
slouken@4998
   248
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
slouken@4996
   249
    }
slouken@4995
   250
slouken@4996
   251
    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
slouken@4995
   252
}
slouken@4995
   253
slouken@4995
   254
extern "C" void Android_JNI_CloseAudioDevice()
slouken@4995
   255
{
slouken@4998
   256
    mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit); 
slouken@4964
   257
slouken@4997
   258
    if (audioBuffer) {
slouken@4997
   259
        mEnv->DeleteGlobalRef(audioBuffer);
slouken@4997
   260
        audioBuffer = NULL;
slouken@4997
   261
        audioBufferPinned = NULL;
slouken@4997
   262
    }
slouken@4964
   263
}
slouken@4981
   264
tim@5650
   265
// Test for an exception and call SDL_SetError with its detail if one occurs
tim@5650
   266
static bool Android_JNI_ExceptionOccurred()
tim@5650
   267
{
tim@5650
   268
    jthrowable exception = mEnv->ExceptionOccurred();
tim@5650
   269
    if (exception != NULL) {
tim@5650
   270
        jmethodID mid;
tim@5650
   271
tim@5650
   272
        // Until this happens most JNI operations have undefined behaviour
tim@5650
   273
        mEnv->ExceptionClear();
tim@5650
   274
tim@5650
   275
        jclass exceptionClass = mEnv->GetObjectClass(exception);
tim@5650
   276
        jclass classClass = mEnv->FindClass("java/lang/Class");
tim@5650
   277
tim@5650
   278
        mid = mEnv->GetMethodID(classClass, "getName", "()Ljava/lang/String;");
tim@5650
   279
        jstring exceptionName = (jstring)mEnv->CallObjectMethod(exceptionClass, mid);
tim@5650
   280
        const char* exceptionNameUTF8 = mEnv->GetStringUTFChars(exceptionName, 0);
tim@5650
   281
tim@5650
   282
        mid = mEnv->GetMethodID(exceptionClass, "getMessage", "()Ljava/lang/String;");
icculus@5860
   283
        jstring exceptionMessage = (jstring)mEnv->CallObjectMethod(exception, mid);
tim@5650
   284
tim@5650
   285
        if (exceptionMessage != NULL) {
tim@5650
   286
            const char* exceptionMessageUTF8 = mEnv->GetStringUTFChars(
tim@5650
   287
                    exceptionMessage, 0);
tim@5650
   288
            SDL_SetError("%s: %s", exceptionNameUTF8, exceptionMessageUTF8);
tim@5650
   289
            mEnv->ReleaseStringUTFChars(exceptionMessage, exceptionMessageUTF8);
tim@5650
   290
            mEnv->DeleteLocalRef(exceptionMessage);
tim@5650
   291
        } else {
tim@5650
   292
            SDL_SetError("%s", exceptionNameUTF8);
tim@5650
   293
        }
tim@5650
   294
tim@5650
   295
        mEnv->ReleaseStringUTFChars(exceptionName, exceptionNameUTF8);
tim@5650
   296
        mEnv->DeleteLocalRef(exceptionName);
tim@5650
   297
        mEnv->DeleteLocalRef(classClass);
tim@5650
   298
        mEnv->DeleteLocalRef(exceptionClass);
tim@5650
   299
        mEnv->DeleteLocalRef(exception);
tim@5650
   300
tim@5650
   301
        return true;
tim@5650
   302
    }
tim@5650
   303
tim@5650
   304
    return false;
tim@5650
   305
}
tim@5650
   306
icculus@5582
   307
static int Android_JNI_FileOpen(SDL_RWops* ctx)
icculus@5582
   308
{
tim@5650
   309
    int result = 0;
tim@5650
   310
tim@5650
   311
    jmethodID mid;
tim@5650
   312
    jobject context;
tim@5650
   313
    jobject assetManager;
tim@5650
   314
    jobject inputStream;
tim@5650
   315
    jclass channels;
tim@5650
   316
    jobject readableByteChannel;
tim@5650
   317
    jstring fileNameJString;
tim@5650
   318
tim@5650
   319
    bool allocatedLocalFrame = false;
tim@5650
   320
tim@5650
   321
    if (mEnv->PushLocalFrame(16) < 0) {
tim@5650
   322
        SDL_SetError("Failed to allocate enough JVM local references");
tim@5650
   323
        goto failure;
tim@5650
   324
    } else {
tim@5650
   325
        allocatedLocalFrame = true;
tim@5650
   326
    }
tim@5650
   327
tim@5650
   328
    fileNameJString = (jstring)ctx->hidden.androidio.fileName;
icculus@5582
   329
icculus@5582
   330
    // context = SDLActivity.getContext();
tim@5650
   331
    mid = mEnv->GetStaticMethodID(mActivityClass,
icculus@5582
   332
            "getContext","()Landroid/content/Context;");
tim@5650
   333
    context = mEnv->CallStaticObjectMethod(mActivityClass, mid);
icculus@5582
   334
icculus@5582
   335
    // assetManager = context.getAssets();
icculus@5582
   336
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(context),
tim@5650
   337
            "getAssets", "()Landroid/content/res/AssetManager;");
tim@5650
   338
    assetManager = mEnv->CallObjectMethod(context, mid);
icculus@5582
   339
icculus@5582
   340
    // inputStream = assetManager.open(<filename>);
icculus@5582
   341
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(assetManager),
icculus@5582
   342
            "open", "(Ljava/lang/String;)Ljava/io/InputStream;");
tim@5650
   343
    inputStream = mEnv->CallObjectMethod(assetManager, mid, fileNameJString);
tim@5650
   344
    if (Android_JNI_ExceptionOccurred()) {
tim@5650
   345
        goto failure;
icculus@5582
   346
    }
icculus@5582
   347
tim@5650
   348
    ctx->hidden.androidio.inputStream = inputStream;
tim@5650
   349
    ctx->hidden.androidio.inputStreamRef = mEnv->NewGlobalRef(inputStream);
tim@5650
   350
icculus@5582
   351
    // Despite all the visible documentation on [Asset]InputStream claiming
icculus@5582
   352
    // that the .available() method is not guaranteed to return the entire file
icculus@5582
   353
    // size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
icculus@5582
   354
    // android/apis/content/ReadAsset.java imply that Android's
icculus@5582
   355
    // AssetInputStream.available() /will/ always return the total file size
icculus@5582
   356
icculus@5582
   357
    // size = inputStream.available();
icculus@5582
   358
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
icculus@5582
   359
            "available", "()I");
icculus@5582
   360
    ctx->hidden.androidio.size = mEnv->CallIntMethod(inputStream, mid);
tim@5650
   361
    if (Android_JNI_ExceptionOccurred()) {
tim@5650
   362
        goto failure;
icculus@5582
   363
    }
icculus@5582
   364
icculus@5582
   365
    // readableByteChannel = Channels.newChannel(inputStream);
tim@5650
   366
    channels = mEnv->FindClass("java/nio/channels/Channels");
icculus@5582
   367
    mid = mEnv->GetStaticMethodID(channels,
icculus@5582
   368
            "newChannel",
icculus@5582
   369
            "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
tim@5650
   370
    readableByteChannel = mEnv->CallStaticObjectMethod(
icculus@5582
   371
            channels, mid, inputStream);
tim@5650
   372
    if (Android_JNI_ExceptionOccurred()) {
tim@5650
   373
        goto failure;
icculus@5582
   374
    }
icculus@5582
   375
tim@5650
   376
    ctx->hidden.androidio.readableByteChannel = readableByteChannel;
tim@5650
   377
    ctx->hidden.androidio.readableByteChannelRef =
tim@5650
   378
        mEnv->NewGlobalRef(readableByteChannel);
tim@5650
   379
icculus@5582
   380
    // Store .read id for reading purposes
icculus@5582
   381
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(readableByteChannel),
icculus@5582
   382
            "read", "(Ljava/nio/ByteBuffer;)I");
icculus@5582
   383
    ctx->hidden.androidio.readMethod = mid;
icculus@5582
   384
icculus@5582
   385
    ctx->hidden.androidio.position = 0;
icculus@5582
   386
tim@5650
   387
    if (false) {
tim@5650
   388
failure:
tim@5650
   389
        result = -1;
tim@5650
   390
tim@5650
   391
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
tim@5650
   392
tim@5650
   393
        if(ctx->hidden.androidio.inputStreamRef != NULL) {
tim@5650
   394
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
tim@5650
   395
        }
tim@5650
   396
    }
tim@5650
   397
tim@5650
   398
    if (allocatedLocalFrame) {
tim@5650
   399
        mEnv->PopLocalFrame(NULL);
tim@5650
   400
    }
tim@5650
   401
tim@5650
   402
    return result;
icculus@5582
   403
}
icculus@5582
   404
icculus@5582
   405
extern "C" int Android_JNI_FileOpen(SDL_RWops* ctx,
icculus@5582
   406
        const char* fileName, const char*)
icculus@5582
   407
{
icculus@5582
   408
    if (!ctx) {
icculus@5582
   409
        return -1;
icculus@5582
   410
    }
icculus@5582
   411
icculus@5582
   412
    jstring fileNameJString = mEnv->NewStringUTF(fileName);
icculus@5582
   413
    ctx->hidden.androidio.fileName = fileNameJString;
icculus@5582
   414
    ctx->hidden.androidio.fileNameRef = mEnv->NewGlobalRef(fileNameJString);
tim@5650
   415
    ctx->hidden.androidio.inputStreamRef = NULL;
tim@5650
   416
    mEnv->DeleteLocalRef(fileNameJString);
icculus@5582
   417
icculus@5582
   418
    return Android_JNI_FileOpen(ctx);
icculus@5582
   419
}
icculus@5582
   420
icculus@5582
   421
extern "C" size_t Android_JNI_FileRead(SDL_RWops* ctx, void* buffer,
icculus@5582
   422
        size_t size, size_t maxnum)
icculus@5582
   423
{
icculus@5582
   424
    int bytesRemaining = size * maxnum;
icculus@5582
   425
    int bytesRead = 0;
icculus@5582
   426
icculus@5582
   427
    jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannel;
icculus@5582
   428
    jmethodID readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
icculus@5582
   429
    jobject byteBuffer = mEnv->NewDirectByteBuffer(buffer, bytesRemaining);
icculus@5582
   430
icculus@5582
   431
    while (bytesRemaining > 0) {
icculus@5582
   432
        // result = readableByteChannel.read(...);
icculus@5582
   433
        int result = mEnv->CallIntMethod(readableByteChannel, readMethod, byteBuffer);
icculus@5582
   434
tim@5650
   435
        if (Android_JNI_ExceptionOccurred()) {
tim@5650
   436
            mEnv->DeleteLocalRef(byteBuffer);
icculus@5582
   437
            return 0;
icculus@5582
   438
        }
icculus@5582
   439
icculus@5582
   440
        if (result < 0) {
icculus@5582
   441
            break;
icculus@5582
   442
        }
icculus@5582
   443
icculus@5582
   444
        bytesRemaining -= result;
icculus@5582
   445
        bytesRead += result;
icculus@5582
   446
        ctx->hidden.androidio.position += result;
icculus@5582
   447
    }
icculus@5582
   448
tim@5650
   449
    mEnv->DeleteLocalRef(byteBuffer);
tim@5650
   450
icculus@5582
   451
    return bytesRead / size;
icculus@5582
   452
}
icculus@5582
   453
icculus@5582
   454
extern "C" size_t Android_JNI_FileWrite(SDL_RWops* ctx, const void* buffer,
icculus@5582
   455
        size_t size, size_t num)
icculus@5582
   456
{
icculus@5582
   457
    SDL_SetError("Cannot write to Android package filesystem");
icculus@5582
   458
    return 0;
icculus@5582
   459
}
icculus@5582
   460
icculus@5582
   461
static int Android_JNI_FileClose(SDL_RWops* ctx, bool release)
icculus@5582
   462
{
icculus@5582
   463
    int result = 0;
icculus@5582
   464
icculus@5582
   465
    if (ctx) {
icculus@5582
   466
        if (release) {
icculus@5582
   467
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
icculus@5582
   468
        }
icculus@5582
   469
icculus@5582
   470
        jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
icculus@5582
   471
icculus@5582
   472
        // inputStream.close();
icculus@5582
   473
        jmethodID mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
icculus@5582
   474
                "close", "()V");
icculus@5582
   475
        mEnv->CallVoidMethod(inputStream, mid);
icculus@5582
   476
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
icculus@5582
   477
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.readableByteChannelRef);
tim@5650
   478
        if (Android_JNI_ExceptionOccurred()) {
icculus@5582
   479
            result = -1;
icculus@5582
   480
        }
icculus@5582
   481
icculus@5582
   482
        if (release) {
icculus@5582
   483
            SDL_FreeRW(ctx);
icculus@5582
   484
        }
icculus@5582
   485
    }
icculus@5582
   486
icculus@5582
   487
    return result;
icculus@5582
   488
}
icculus@5582
   489
icculus@5582
   490
icculus@5582
   491
extern "C" long Android_JNI_FileSeek(SDL_RWops* ctx, long offset, int whence)
icculus@5582
   492
{
icculus@5582
   493
    long newPosition;
icculus@5582
   494
icculus@5582
   495
    switch (whence) {
icculus@5582
   496
        case RW_SEEK_SET:
icculus@5582
   497
            newPosition = offset;
icculus@5582
   498
            break;
icculus@5582
   499
        case RW_SEEK_CUR:
icculus@5582
   500
            newPosition = ctx->hidden.androidio.position + offset;
icculus@5582
   501
            break;
icculus@5582
   502
        case RW_SEEK_END:
icculus@5582
   503
            newPosition = ctx->hidden.androidio.size + offset;
icculus@5582
   504
            break;
icculus@5582
   505
        default:
icculus@5582
   506
            SDL_SetError("Unknown value for 'whence'");
icculus@5582
   507
            return -1;
icculus@5582
   508
    }
icculus@5582
   509
    if (newPosition < 0) {
icculus@5582
   510
        newPosition = 0;
icculus@5582
   511
    }
icculus@5582
   512
    if (newPosition > ctx->hidden.androidio.size) {
icculus@5582
   513
        newPosition = ctx->hidden.androidio.size;
icculus@5582
   514
    }
icculus@5582
   515
icculus@5582
   516
    long movement = newPosition - ctx->hidden.androidio.position;
icculus@5582
   517
    jobject inputStream = (jobject)ctx->hidden.androidio.inputStream;
icculus@5582
   518
icculus@5582
   519
    if (movement > 0) {
tim@5993
   520
        unsigned char buffer[1024];
tim@5993
   521
icculus@5582
   522
        // The easy case where we're seeking forwards
icculus@5582
   523
        while (movement > 0) {
tim@5993
   524
            size_t result = Android_JNI_FileRead(ctx, buffer, 1,
tim@5993
   525
                std::min(movement, (long)sizeof(buffer)));
tim@5993
   526
tim@5993
   527
            if (result <= 0) {
tim@5993
   528
                // Failed to read/skip the required amount, so fail
icculus@5582
   529
                return -1;
icculus@5582
   530
            }
tim@5993
   531
tim@5993
   532
            movement -= result;
icculus@5582
   533
        }
icculus@5582
   534
    } else if (movement < 0) {
icculus@5582
   535
        // We can't seek backwards so we have to reopen the file and seek
icculus@5582
   536
        // forwards which obviously isn't very efficient
icculus@5582
   537
        Android_JNI_FileClose(ctx, false);
icculus@5582
   538
        Android_JNI_FileOpen(ctx);
icculus@5582
   539
        Android_JNI_FileSeek(ctx, newPosition, RW_SEEK_SET);
icculus@5582
   540
    }
icculus@5582
   541
icculus@5582
   542
    ctx->hidden.androidio.position = newPosition;
icculus@5582
   543
icculus@5582
   544
    return ctx->hidden.androidio.position;
icculus@5582
   545
}
icculus@5582
   546
icculus@5582
   547
extern "C" int Android_JNI_FileClose(SDL_RWops* ctx)
icculus@5582
   548
{
icculus@5582
   549
    return Android_JNI_FileClose(ctx, true);
icculus@5582
   550
}
icculus@5582
   551
slouken@4981
   552
/* vi: set ts=4 sw=4 expandtab: */