src/core/android/SDL_android.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 03 Jun 2017 23:00:40 +0200
changeset 11061 b860259d72e5
parent 11031 b1475dc05bd0
child 11066 f39b145db3eb
permissions -rw-r--r--
android: Moved internal function to new position.

It was grouped with functions for the public system header.
slouken@4964
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@5222
    22
#include "SDL_stdinc.h"
slouken@6284
    23
#include "SDL_assert.h"
slouken@8961
    24
#include "SDL_hints.h"
slouken@6650
    25
#include "SDL_log.h"
slouken@4964
    26
slouken@6044
    27
#ifdef __ANDROID__
slouken@6044
    28
slouken@6630
    29
#include "SDL_system.h"
slouken@4989
    30
#include "SDL_android.h"
slouken@6792
    31
#include <EGL/egl.h>
slouken@4989
    32
slouken@5092
    33
#include "../../events/SDL_events_c.h"
slouken@5092
    34
#include "../../video/android/SDL_androidkeyboard.h"
joseba@9438
    35
#include "../../video/android/SDL_androidmouse.h"
slouken@5092
    36
#include "../../video/android/SDL_androidtouch.h"
slouken@5092
    37
#include "../../video/android/SDL_androidvideo.h"
gabomdq@7659
    38
#include "../../video/android/SDL_androidwindow.h"
gabomdq@8057
    39
#include "../../joystick/android/SDL_sysjoystick_c.h"
slouken@4995
    40
icculus@5996
    41
#include <android/log.h>
gabomdq@6354
    42
#include <pthread.h>
gabomdq@6806
    43
#include <sys/types.h>
gabomdq@6806
    44
#include <unistd.h>
slouken@10672
    45
/* #define LOG_TAG "SDL_android" */
gabomdq@7678
    46
/* #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) */
gabomdq@7678
    47
/* #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) */
philipp@9271
    48
#define LOGI(...) do {} while (0)
philipp@9271
    49
#define LOGE(...) do {} while (0)
icculus@5996
    50
slouken@10672
    51
slouken@10672
    52
#define SDL_JAVA_PREFIX                                 org_libsdl_app
slouken@10672
    53
#define CONCAT1(prefix, class, function)                CONCAT2(prefix, class, function)
slouken@10672
    54
#define CONCAT2(prefix, class, function)                Java_ ## prefix ## _ ## class ## _ ## function
slouken@10672
    55
#define SDL_JAVA_INTERFACE(function)                    CONCAT1(SDL_JAVA_PREFIX, SDLActivity, function)
slouken@10672
    56
#define SDL_JAVA_INTERFACE_INPUT_CONNECTION(function)   CONCAT1(SDL_JAVA_PREFIX, SDLInputConnection, function)
slouken@10672
    57
slouken@10672
    58
slouken@10672
    59
/* Java class SDLActivity */
slouken@10672
    60
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeDropFile)(
slouken@10672
    61
        JNIEnv* env, jclass jcls,
slouken@10672
    62
        jstring filename);
slouken@10672
    63
slouken@10672
    64
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
slouken@10672
    65
        JNIEnv* env, jclass jcls,
slouken@10672
    66
        jint width, jint height, jint format, jfloat rate);
slouken@10672
    67
slouken@10672
    68
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(onNativePadDown)(
slouken@10672
    69
        JNIEnv* env, jclass jcls,
slouken@10672
    70
        jint device_id, jint keycode);
slouken@10672
    71
slouken@10672
    72
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(onNativePadUp)(
slouken@10672
    73
        JNIEnv* env, jclass jcls,
slouken@10672
    74
        jint device_id, jint keycode);
slouken@10672
    75
slouken@10672
    76
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeJoy)(
slouken@10672
    77
        JNIEnv* env, jclass jcls,
slouken@10672
    78
        jint device_id, jint axis, jfloat value);
slouken@10672
    79
slouken@10672
    80
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeHat)(
slouken@10672
    81
        JNIEnv* env, jclass jcls,
slouken@10672
    82
        jint device_id, jint hat_id, jint x, jint y);
slouken@10672
    83
slouken@10672
    84
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(nativeAddJoystick)(
slouken@10672
    85
        JNIEnv* env, jclass jcls,
slouken@10672
    86
        jint device_id, jstring device_name, jint is_accelerometer, 
slouken@10672
    87
        jint nbuttons, jint naxes, jint nhats, jint nballs);
slouken@10672
    88
slouken@10672
    89
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(nativeRemoveJoystick)(
slouken@10672
    90
        JNIEnv* env, jclass jcls,
slouken@10672
    91
        jint device_id);
slouken@10672
    92
slouken@10672
    93
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(
slouken@10672
    94
        JNIEnv* env, jclass jcls);
slouken@10672
    95
slouken@10672
    96
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceDestroyed)(
slouken@10672
    97
        JNIEnv* env, jclass jcls);
slouken@10672
    98
slouken@10672
    99
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeKeyDown)(
slouken@10672
   100
        JNIEnv* env, jclass jcls,
slouken@10672
   101
        jint keycode);
slouken@10672
   102
slouken@10672
   103
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeKeyUp)(
slouken@10672
   104
        JNIEnv* env, jclass jcls,
slouken@10672
   105
        jint keycode);
slouken@10672
   106
slouken@10672
   107
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeKeyboardFocusLost)(
slouken@10672
   108
        JNIEnv* env, jclass jcls);
slouken@10672
   109
slouken@10672
   110
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeTouch)(
slouken@10672
   111
        JNIEnv* env, jclass jcls,
slouken@10672
   112
        jint touch_device_id_in, jint pointer_finger_id_in,
slouken@10672
   113
        jint action, jfloat x, jfloat y, jfloat p);
slouken@10672
   114
slouken@10672
   115
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
slouken@10672
   116
        JNIEnv* env, jclass jcls,
slouken@10672
   117
        jint button, jint action, jfloat x, jfloat y);
slouken@10672
   118
slouken@10672
   119
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeAccel)(
slouken@10672
   120
        JNIEnv* env, jclass jcls,
slouken@10672
   121
        jfloat x, jfloat y, jfloat z);
slouken@10672
   122
slouken@10672
   123
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeLowMemory)(
slouken@10672
   124
        JNIEnv* env, jclass cls);
slouken@10672
   125
slouken@10672
   126
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeQuit)(
slouken@10672
   127
        JNIEnv* env, jclass cls);
slouken@10672
   128
slouken@10672
   129
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePause)(
slouken@10672
   130
        JNIEnv* env, jclass cls);
slouken@10672
   131
slouken@10672
   132
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeResume)(
slouken@10672
   133
        JNIEnv* env, jclass cls);
slouken@10672
   134
slouken@10672
   135
JNIEXPORT jstring JNICALL SDL_JAVA_INTERFACE(nativeGetHint)(
slouken@10672
   136
        JNIEnv* env, jclass cls,
slouken@10672
   137
        jstring name);
slouken@10672
   138
slouken@10672
   139
/* Java class SDLInputConnection */
slouken@10672
   140
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)(
slouken@10672
   141
        JNIEnv* env, jclass cls,
slouken@10672
   142
        jstring text, jint newCursorPosition);
slouken@10672
   143
slouken@10672
   144
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeSetComposingText)(
slouken@10672
   145
        JNIEnv* env, jclass cls,
slouken@10672
   146
        jstring text, jint newCursorPosition);
slouken@10672
   147
slouken@10672
   148
slouken@6650
   149
/* Uncomment this to log messages entering and exiting methods in this file */
gabomdq@7678
   150
/* #define DEBUG_JNI */
icculus@5996
   151
philipp@7246
   152
static void Android_JNI_ThreadDestroyed(void*);
slouken@4980
   153
slouken@4964
   154
/*******************************************************************************
slouken@4964
   155
 This file links the Java side of Android with libsdl
slouken@4964
   156
*******************************************************************************/
slouken@4964
   157
#include <jni.h>
slouken@4964
   158
#include <android/log.h>
slouken@4964
   159
slouken@4964
   160
slouken@4964
   161
/*******************************************************************************
slouken@4964
   162
                               Globals
slouken@4964
   163
*******************************************************************************/
gabomdq@6354
   164
static pthread_key_t mThreadKey;
icculus@5996
   165
static JavaVM* mJavaVM;
slouken@4964
   166
gabomdq@7663
   167
/* Main activity */
slouken@4998
   168
static jclass mActivityClass;
slouken@4964
   169
gabomdq@7663
   170
/* method signatures */
gabomdq@7659
   171
static jmethodID midGetNativeSurface;
icculus@10280
   172
static jmethodID midAudioOpen;
slouken@4995
   173
static jmethodID midAudioWriteShortBuffer;
slouken@4995
   174
static jmethodID midAudioWriteByteBuffer;
icculus@10280
   175
static jmethodID midAudioClose;
icculus@10280
   176
static jmethodID midCaptureOpen;
icculus@10280
   177
static jmethodID midCaptureReadShortBuffer;
icculus@10280
   178
static jmethodID midCaptureReadByteBuffer;
icculus@10280
   179
static jmethodID midCaptureClose;
gabomdq@8057
   180
static jmethodID midPollInputDevices;
slouken@4964
   181
gabomdq@7663
   182
/* Accelerometer data storage */
slouken@5000
   183
static float fLastAccelerometer[3];
philipp@9271
   184
static SDL_bool bHasNewData;
slouken@4964
   185
slouken@4964
   186
/*******************************************************************************
slouken@4964
   187
                 Functions called by JNI
slouken@4964
   188
*******************************************************************************/
slouken@4964
   189
gabomdq@7663
   190
/* Library init */
dimitris@8760
   191
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
slouken@4964
   192
{
icculus@5996
   193
    JNIEnv *env;
icculus@5996
   194
    mJavaVM = vm;
icculus@5996
   195
    LOGI("JNI_OnLoad called");
ewing@7501
   196
    if ((*mJavaVM)->GetEnv(mJavaVM, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
icculus@5996
   197
        LOGE("Failed to get the environment using GetEnv()");
icculus@5996
   198
        return -1;
icculus@5996
   199
    }
gabomdq@6354
   200
    /*
gabomdq@6354
   201
     * Create mThreadKey so we can keep track of the JNIEnv assigned to each thread
gabomdq@6354
   202
     * Refer to http://developer.android.com/guide/practices/design/jni.html for the rationale behind this
gabomdq@6354
   203
     */
slouken@8055
   204
    if (pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed) != 0) {
gabomdq@6354
   205
        __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing pthread key");
gabomdq@6354
   206
    }
slouken@8055
   207
    Android_JNI_SetupThread();
icculus@5996
   208
slouken@4964
   209
    return JNI_VERSION_1_4;
slouken@4964
   210
}
slouken@4964
   211
gabomdq@7663
   212
/* Called before SDL_main() to initialize JNI bindings */
dimitris@8760
   213
JNIEXPORT void JNICALL SDL_Android_Init(JNIEnv* mEnv, jclass cls)
slouken@4964
   214
{
slouken@4964
   215
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
   216
gabomdq@6354
   217
    Android_JNI_SetupThread();
gabomdq@6354
   218
ewing@7501
   219
    mActivityClass = (jclass)((*mEnv)->NewGlobalRef(mEnv, cls));
slouken@4998
   220
gabomdq@7659
   221
    midGetNativeSurface = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
gabomdq@7659
   222
                                "getNativeSurface","()Landroid/view/Surface;");
icculus@10280
   223
    midAudioOpen = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
icculus@10280
   224
                                "audioOpen", "(IZZI)I");
ewing@7501
   225
    midAudioWriteShortBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
slouken@4998
   226
                                "audioWriteShortBuffer", "([S)V");
ewing@7501
   227
    midAudioWriteByteBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
slouken@4998
   228
                                "audioWriteByteBuffer", "([B)V");
icculus@10280
   229
    midAudioClose = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
icculus@10280
   230
                                "audioClose", "()V");
icculus@10280
   231
    midCaptureOpen = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
icculus@10280
   232
                                "captureOpen", "(IZZI)I");
icculus@10280
   233
    midCaptureReadShortBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
icculus@10280
   234
                                "captureReadShortBuffer", "([SZ)I");
icculus@10280
   235
    midCaptureReadByteBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
icculus@10280
   236
                                "captureReadByteBuffer", "([BZ)I");
icculus@10280
   237
    midCaptureClose = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
icculus@10280
   238
                                "captureClose", "()V");
gabomdq@8057
   239
    midPollInputDevices = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
gabomdq@8057
   240
                                "pollInputDevices", "()V");
slouken@4964
   241
philipp@9271
   242
    bHasNewData = SDL_FALSE;
slouken@6212
   243
icculus@10280
   244
    if (!midGetNativeSurface ||
philipp@10296
   245
       !midAudioOpen || !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioClose ||
icculus@10280
   246
       !midCaptureOpen || !midCaptureReadShortBuffer || !midCaptureReadByteBuffer || !midCaptureClose ||
icculus@10280
   247
       !midPollInputDevices) {
slouken@4996
   248
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
slouken@4964
   249
    }
icculus@5996
   250
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
slouken@4964
   251
}
slouken@4964
   252
icculus@9640
   253
/* Drop file */
slouken@10672
   254
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeDropFile)(
icculus@9640
   255
                                    JNIEnv* env, jclass jcls,
icculus@9640
   256
                                    jstring filename)
icculus@9640
   257
{
icculus@9640
   258
    const char *path = (*env)->GetStringUTFChars(env, filename, NULL);
icculus@10032
   259
    SDL_SendDropFile(NULL, path);
icculus@9640
   260
    (*env)->ReleaseStringUTFChars(env, filename, path);
icculus@10032
   261
    SDL_SendDropComplete(NULL);
icculus@9640
   262
}
icculus@9640
   263
gabomdq@7663
   264
/* Resize */
slouken@10672
   265
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
slouken@4998
   266
                                    JNIEnv* env, jclass jcls,
philipp@9314
   267
                                    jint width, jint height, jint format, jfloat rate)
slouken@4995
   268
{
philipp@9314
   269
    Android_SetScreenResolution(width, height, format, rate);
slouken@4995
   270
}
slouken@4995
   271
philipp@8776
   272
/* Paddown */
slouken@10672
   273
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(onNativePadDown)(
gabomdq@7907
   274
                                    JNIEnv* env, jclass jcls,
gabomdq@8057
   275
                                    jint device_id, jint keycode)
gabomdq@7907
   276
{
gabomdq@8057
   277
    return Android_OnPadDown(device_id, keycode);
gabomdq@7907
   278
}
gabomdq@7907
   279
philipp@8776
   280
/* Padup */
slouken@10672
   281
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(onNativePadUp)(
slouken@10672
   282
                                    JNIEnv* env, jclass jcls,
slouken@10672
   283
                                    jint device_id, jint keycode)
gabomdq@7907
   284
{
gabomdq@8057
   285
    return Android_OnPadUp(device_id, keycode);
gabomdq@7907
   286
}
gabomdq@7907
   287
philipp@7926
   288
/* Joy */
slouken@10672
   289
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeJoy)(
gabomdq@7907
   290
                                    JNIEnv* env, jclass jcls,
gabomdq@8057
   291
                                    jint device_id, jint axis, jfloat value)
gabomdq@7907
   292
{
gabomdq@8057
   293
    Android_OnJoy(device_id, axis, value);
gabomdq@8057
   294
}
gabomdq@8057
   295
dbrady@8140
   296
/* POV Hat */
slouken@10672
   297
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeHat)(
dbrady@8140
   298
                                    JNIEnv* env, jclass jcls,
dbrady@8140
   299
                                    jint device_id, jint hat_id, jint x, jint y)
dbrady@8140
   300
{
dbrady@8140
   301
    Android_OnHat(device_id, hat_id, x, y);
dbrady@8140
   302
}
dbrady@8140
   303
gabomdq@8057
   304
slouken@10672
   305
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(nativeAddJoystick)(
slouken@10672
   306
                                    JNIEnv* env, jclass jcls,
slouken@10672
   307
                                    jint device_id, jstring device_name, jint is_accelerometer, 
slouken@10672
   308
                                    jint nbuttons, jint naxes, jint nhats, jint nballs)
gabomdq@8057
   309
{
gabomdq@8057
   310
    int retval;
gabomdq@8057
   311
    const char *name = (*env)->GetStringUTFChars(env, device_name, NULL);
gabomdq@8057
   312
gabomdq@8057
   313
    retval = Android_AddJoystick(device_id, name, (SDL_bool) is_accelerometer, nbuttons, naxes, nhats, nballs);
gabomdq@8057
   314
gabomdq@8057
   315
    (*env)->ReleaseStringUTFChars(env, device_name, name);
gabomdq@8057
   316
    
gabomdq@8057
   317
    return retval;
gabomdq@8057
   318
}
gabomdq@8057
   319
slouken@10672
   320
JNIEXPORT jint JNICALL SDL_JAVA_INTERFACE(nativeRemoveJoystick)(
slouken@10672
   321
                                    JNIEnv* env, jclass jcls,
slouken@10672
   322
                                    jint device_id)
gabomdq@8057
   323
{
gabomdq@8057
   324
    return Android_RemoveJoystick(device_id);
gabomdq@7907
   325
}
gabomdq@7907
   326
gabomdq@7659
   327
gabomdq@7663
   328
/* Surface Created */
slouken@10672
   329
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(JNIEnv* env, jclass jcls)
gabomdq@7659
   330
{
gabomdq@7659
   331
    SDL_WindowData *data;
gabomdq@7659
   332
    SDL_VideoDevice *_this;
gabomdq@7659
   333
gabomdq@7659
   334
    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
gabomdq@7659
   335
        return;
gabomdq@7659
   336
    }
gabomdq@7659
   337
    
gabomdq@7659
   338
    _this =  SDL_GetVideoDevice();
gabomdq@7659
   339
    data =  (SDL_WindowData *) Android_Window->driverdata;
gabomdq@7659
   340
    
gabomdq@7659
   341
    /* If the surface has been previously destroyed by onNativeSurfaceDestroyed, recreate it here */
gabomdq@7659
   342
    if (data->egl_surface == EGL_NO_SURFACE) {
gabomdq@7659
   343
        if(data->native_window) {
gabomdq@7659
   344
            ANativeWindow_release(data->native_window);
gabomdq@7659
   345
        }
gabomdq@7659
   346
        data->native_window = Android_JNI_GetNativeWindow();
gabomdq@7659
   347
        data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
gabomdq@7659
   348
    }
gabomdq@7659
   349
    
gabomdq@7659
   350
    /* GL Context handling is done in the event loop because this function is run from the Java thread */
gabomdq@7659
   351
    
gabomdq@7659
   352
}
gabomdq@7659
   353
gabomdq@7663
   354
/* Surface Destroyed */
slouken@10672
   355
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceDestroyed)(JNIEnv* env, jclass jcls)
gabomdq@7659
   356
{
gabomdq@7659
   357
    /* We have to clear the current context and destroy the egl surface here
gabomdq@7659
   358
     * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume
gabomdq@7659
   359
     * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d
gabomdq@7659
   360
     */
gabomdq@7659
   361
    SDL_WindowData *data;
gabomdq@7659
   362
    SDL_VideoDevice *_this;
gabomdq@7659
   363
    
gabomdq@7659
   364
    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
gabomdq@7659
   365
        return;
gabomdq@7659
   366
    }
gabomdq@7659
   367
    
gabomdq@7659
   368
    _this =  SDL_GetVideoDevice();
gabomdq@7659
   369
    data = (SDL_WindowData *) Android_Window->driverdata;
gabomdq@7659
   370
    
gabomdq@7659
   371
    if (data->egl_surface != EGL_NO_SURFACE) {
gabomdq@7659
   372
        SDL_EGL_MakeCurrent(_this, NULL, NULL);
gabomdq@7659
   373
        SDL_EGL_DestroySurface(_this, data->egl_surface);
gabomdq@7659
   374
        data->egl_surface = EGL_NO_SURFACE;
gabomdq@7659
   375
    }
gabomdq@7659
   376
    
gabomdq@7659
   377
    /* GL Context handling is done in the event loop because this function is run from the Java thread */
gabomdq@7659
   378
gabomdq@7659
   379
}
gabomdq@7659
   380
gabomdq@7663
   381
/* Keydown */
slouken@10672
   382
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeKeyDown)(
slouken@10672
   383
                                    JNIEnv* env, jclass jcls,
slouken@10672
   384
                                    jint keycode)
slouken@4964
   385
{
slouken@4980
   386
    Android_OnKeyDown(keycode);
slouken@4964
   387
}
slouken@4964
   388
gabomdq@7663
   389
/* Keyup */
slouken@10672
   390
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeKeyUp)(
slouken@10672
   391
                                    JNIEnv* env, jclass jcls,
slouken@10672
   392
                                    jint keycode)
slouken@4964
   393
{
slouken@4980
   394
    Android_OnKeyUp(keycode);
slouken@4964
   395
}
slouken@4964
   396
gabomdq@7663
   397
/* Keyboard Focus Lost */
slouken@10672
   398
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeKeyboardFocusLost)(
gabomdq@7564
   399
                                    JNIEnv* env, jclass jcls)
gabomdq@7564
   400
{
gabomdq@7564
   401
    /* Calling SDL_StopTextInput will take care of hiding the keyboard and cleaning up the DummyText widget */
gabomdq@7564
   402
    SDL_StopTextInput();
gabomdq@7564
   403
}
gabomdq@7564
   404
gabomdq@7564
   405
gabomdq@7663
   406
/* Touch */
slouken@10672
   407
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeTouch)(
slouken@4998
   408
                                    JNIEnv* env, jclass jcls,
icculus@5982
   409
                                    jint touch_device_id_in, jint pointer_finger_id_in,
slouken@4995
   410
                                    jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   411
{
icculus@5982
   412
    Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p);
slouken@4964
   413
}
slouken@4964
   414
joseba@9438
   415
/* Mouse */
slouken@10672
   416
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeMouse)(
joseba@9438
   417
                                    JNIEnv* env, jclass jcls,
joseba@9438
   418
                                    jint button, jint action, jfloat x, jfloat y)
joseba@9438
   419
{
joseba@9438
   420
    Android_OnMouse(button, action, x, y);
joseba@9438
   421
}
joseba@9438
   422
gabomdq@7663
   423
/* Accelerometer */
slouken@10672
   424
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeAccel)(
slouken@4998
   425
                                    JNIEnv* env, jclass jcls,
slouken@4995
   426
                                    jfloat x, jfloat y, jfloat z)
slouken@4964
   427
{
slouken@4964
   428
    fLastAccelerometer[0] = x;
slouken@4964
   429
    fLastAccelerometer[1] = y;
slouken@6212
   430
    fLastAccelerometer[2] = z;
philipp@9271
   431
    bHasNewData = SDL_TRUE;
slouken@4964
   432
}
slouken@4964
   433
gabomdq@7663
   434
/* Low memory */
slouken@10672
   435
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeLowMemory)(
slouken@7190
   436
                                    JNIEnv* env, jclass cls)
slouken@7191
   437
{
slouken@7190
   438
    SDL_SendAppEvent(SDL_APP_LOWMEMORY);
slouken@7190
   439
}
slouken@7190
   440
gabomdq@7663
   441
/* Quit */
slouken@10672
   442
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeQuit)(
slouken@4998
   443
                                    JNIEnv* env, jclass cls)
slouken@7191
   444
{
gabomdq@7910
   445
    /* Discard previous events. The user should have handled state storage
gabomdq@7910
   446
     * in SDL_APP_WILLENTERBACKGROUND. After nativeQuit() is called, no
gabomdq@7910
   447
     * events other than SDL_QUIT and SDL_APP_TERMINATING should fire */
gabomdq@7910
   448
    SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
gabomdq@7663
   449
    /* Inject a SDL_QUIT event */
slouken@4995
   450
    SDL_SendQuit();
slouken@7190
   451
    SDL_SendAppEvent(SDL_APP_TERMINATING);
gabomdq@7910
   452
    /* Resume the event loop so that the app can catch SDL_QUIT which
gabomdq@7910
   453
     * should now be the top event in the event queue. */
gabomdq@7910
   454
    if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
slouken@4995
   455
}
slouken@4995
   456
gabomdq@7663
   457
/* Pause */
slouken@10672
   458
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePause)(
slouken@6186
   459
                                    JNIEnv* env, jclass cls)
slouken@6186
   460
{
gabomdq@8039
   461
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
slouken@6186
   462
    if (Android_Window) {
slouken@6186
   463
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
slouken@6191
   464
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
gabomdq@8039
   465
        SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
gabomdq@8039
   466
        SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
gabomdq@8039
   467
    
gabomdq@8039
   468
        /* *After* sending the relevant events, signal the pause semaphore 
gabomdq@8039
   469
         * so the event loop knows to pause and (optionally) block itself */
gabomdq@8039
   470
        if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
slouken@6186
   471
    }
slouken@6186
   472
}
slouken@6186
   473
gabomdq@7663
   474
/* Resume */
slouken@10672
   475
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeResume)(
slouken@6186
   476
                                    JNIEnv* env, jclass cls)
slouken@6186
   477
{
slouken@7190
   478
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
slouken@7190
   479
slouken@6186
   480
    if (Android_Window) {
gabomdq@8047
   481
        SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
gabomdq@8047
   482
        SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
gabomdq@8047
   483
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
gabomdq@8047
   484
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
gabomdq@6330
   485
        /* Signal the resume semaphore so the event loop knows to resume and restore the GL Context
gabomdq@6330
   486
         * We can't restore the GL Context here because it needs to be done on the SDL main thread
gabomdq@6330
   487
         * and this function will be called from the Java thread instead.
gabomdq@6330
   488
         */
gabomdq@6330
   489
        if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
slouken@6186
   490
    }
slouken@6186
   491
}
slouken@6186
   492
slouken@10672
   493
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeCommitText)(
slouken@6555
   494
                                    JNIEnv* env, jclass cls,
slouken@6555
   495
                                    jstring text, jint newCursorPosition)
slouken@6555
   496
{
ewing@7501
   497
    const char *utftext = (*env)->GetStringUTFChars(env, text, NULL);
slouken@6555
   498
slouken@6555
   499
    SDL_SendKeyboardText(utftext);
slouken@6555
   500
ewing@7501
   501
    (*env)->ReleaseStringUTFChars(env, text, utftext);
slouken@6555
   502
}
slouken@6555
   503
slouken@10672
   504
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE_INPUT_CONNECTION(nativeSetComposingText)(
slouken@6555
   505
                                    JNIEnv* env, jclass cls,
slouken@6555
   506
                                    jstring text, jint newCursorPosition)
slouken@6555
   507
{
ewing@7501
   508
    const char *utftext = (*env)->GetStringUTFChars(env, text, NULL);
slouken@6555
   509
slouken@6555
   510
    SDL_SendEditingText(utftext, 0, 0);
slouken@6555
   511
ewing@7501
   512
    (*env)->ReleaseStringUTFChars(env, text, utftext);
slouken@6555
   513
}
slouken@6555
   514
slouken@10672
   515
JNIEXPORT jstring JNICALL SDL_JAVA_INTERFACE(nativeGetHint)(
slouken@10672
   516
                                    JNIEnv* env, jclass cls,
slouken@10672
   517
                                    jstring name)
slouken@10672
   518
{
alexey@8896
   519
    const char *utfname = (*env)->GetStringUTFChars(env, name, NULL);
alexey@8896
   520
    const char *hint = SDL_GetHint(utfname);
slouken@6555
   521
alexey@8896
   522
    jstring result = (*env)->NewStringUTF(env, hint);
alexey@8896
   523
    (*env)->ReleaseStringUTFChars(env, name, utfname);
alexey@8896
   524
alexey@8896
   525
    return result;
alexey@8896
   526
}
slouken@6555
   527
slouken@4964
   528
/*******************************************************************************
slouken@4964
   529
             Functions called by SDL into Java
slouken@4964
   530
*******************************************************************************/
slouken@6284
   531
ewing@7501
   532
static int s_active = 0;
ewing@7501
   533
struct LocalReferenceHolder
slouken@6284
   534
{
slouken@6284
   535
    JNIEnv *m_env;
slouken@6650
   536
    const char *m_func;
slouken@6284
   537
};
slouken@6284
   538
ewing@7501
   539
static struct LocalReferenceHolder LocalReferenceHolder_Setup(const char *func)
ewing@7501
   540
{
ewing@7501
   541
    struct LocalReferenceHolder refholder;
ewing@7501
   542
    refholder.m_env = NULL;
ewing@7501
   543
    refholder.m_func = func;
ewing@7501
   544
#ifdef DEBUG_JNI
ewing@7501
   545
    SDL_Log("Entering function %s", func);
ewing@7501
   546
#endif
ewing@7501
   547
    return refholder;
ewing@7501
   548
}
ewing@7501
   549
ewing@7501
   550
static SDL_bool LocalReferenceHolder_Init(struct LocalReferenceHolder *refholder, JNIEnv *env)
ewing@7501
   551
{
ewing@7501
   552
    const int capacity = 16;
ewing@7501
   553
    if ((*env)->PushLocalFrame(env, capacity) < 0) {
ewing@7501
   554
        SDL_SetError("Failed to allocate enough JVM local references");
philipp@7516
   555
        return SDL_FALSE;
ewing@7501
   556
    }
ewing@7501
   557
    ++s_active;
ewing@7501
   558
    refholder->m_env = env;
ewing@7501
   559
    return SDL_TRUE;
ewing@7501
   560
}
ewing@7501
   561
ewing@7501
   562
static void LocalReferenceHolder_Cleanup(struct LocalReferenceHolder *refholder)
ewing@7501
   563
{
ewing@7501
   564
#ifdef DEBUG_JNI
ewing@7501
   565
    SDL_Log("Leaving function %s", refholder->m_func);
ewing@7501
   566
#endif
ewing@7501
   567
    if (refholder->m_env) {
ewing@7501
   568
        JNIEnv* env = refholder->m_env;
ewing@7501
   569
        (*env)->PopLocalFrame(env, NULL);
ewing@7501
   570
        --s_active;
ewing@7501
   571
    }
ewing@7501
   572
}
ewing@7501
   573
philipp@9594
   574
static SDL_bool LocalReferenceHolder_IsActive(void)
ewing@7501
   575
{
gabomdq@9173
   576
    return s_active > 0;
gabomdq@9173
   577
}
gabomdq@9173
   578
gabomdq@7659
   579
ANativeWindow* Android_JNI_GetNativeWindow(void)
slouken@4964
   580
{
gabomdq@7659
   581
    ANativeWindow* anw;
gabomdq@7659
   582
    jobject s;
slouken@6792
   583
    JNIEnv *env = Android_JNI_GetEnv();
slouken@6792
   584
gabomdq@7659
   585
    s = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetNativeSurface);
gabomdq@7659
   586
    anw = ANativeWindow_fromSurface(env, s);
gabomdq@7659
   587
    (*env)->DeleteLocalRef(env, s);
gabomdq@7659
   588
  
gabomdq@7659
   589
    return anw;
gabomdq@7567
   590
}
gabomdq@7567
   591
ewing@7501
   592
void Android_JNI_SetActivityTitle(const char *title)
slouken@4998
   593
{
slouken@4998
   594
    jmethodID mid;
gabomdq@6354
   595
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   596
    mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,"setActivityTitle","(Ljava/lang/String;)Z");
slouken@4998
   597
    if (mid) {
ewing@7501
   598
        jstring jtitle = (jstring)((*mEnv)->NewStringUTF(mEnv, title));
ewing@7501
   599
        (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, mid, jtitle);
ewing@7501
   600
        (*mEnv)->DeleteLocalRef(mEnv, jtitle);
slouken@4998
   601
    }
slouken@4964
   602
}
slouken@4964
   603
ewing@7501
   604
SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
slouken@5000
   605
{
slouken@5000
   606
    int i;
slouken@6212
   607
    SDL_bool retval = SDL_FALSE;
slouken@6212
   608
slouken@6212
   609
    if (bHasNewData) {
slouken@6212
   610
        for (i = 0; i < 3; ++i) {
slouken@6212
   611
            values[i] = fLastAccelerometer[i];
slouken@6212
   612
        }
philipp@9271
   613
        bHasNewData = SDL_FALSE;
slouken@6212
   614
        retval = SDL_TRUE;
slouken@5000
   615
    }
slouken@6212
   616
slouken@6212
   617
    return retval;
slouken@5000
   618
}
slouken@5000
   619
slouken@8055
   620
static void Android_JNI_ThreadDestroyed(void* value)
slouken@8055
   621
{
gabomdq@6354
   622
    /* The thread is being destroyed, detach it from the Java VM and set the mThreadKey value to NULL as required */
gabomdq@6354
   623
    JNIEnv *env = (JNIEnv*) value;
gabomdq@6354
   624
    if (env != NULL) {
ewing@7501
   625
        (*mJavaVM)->DetachCurrentThread(mJavaVM);
gabomdq@6354
   626
        pthread_setspecific(mThreadKey, NULL);
gabomdq@6354
   627
    }
gabomdq@6354
   628
}
gabomdq@6354
   629
slouken@8055
   630
JNIEnv* Android_JNI_GetEnv(void)
slouken@8055
   631
{
gabomdq@6354
   632
    /* From http://developer.android.com/guide/practices/jni.html
gabomdq@6354
   633
     * All threads are Linux threads, scheduled by the kernel.
gabomdq@6354
   634
     * They're usually started from managed code (using Thread.start), but they can also be created elsewhere and then
gabomdq@6354
   635
     * attached to the JavaVM. For example, a thread started with pthread_create can be attached with the
gabomdq@6354
   636
     * JNI AttachCurrentThread or AttachCurrentThreadAsDaemon functions. Until a thread is attached, it has no JNIEnv,
gabomdq@6354
   637
     * and cannot make JNI calls.
gabomdq@6354
   638
     * Attaching a natively-created thread causes a java.lang.Thread object to be constructed and added to the "main"
gabomdq@6354
   639
     * ThreadGroup, making it visible to the debugger. Calling AttachCurrentThread on an already-attached thread
gabomdq@6354
   640
     * is a no-op.
gabomdq@6354
   641
     * Note: You can call this function any number of times for the same thread, there's no harm in it
gabomdq@6354
   642
     */
gabomdq@6354
   643
gabomdq@6354
   644
    JNIEnv *env;
ewing@7501
   645
    int status = (*mJavaVM)->AttachCurrentThread(mJavaVM, &env, NULL);
gabomdq@6354
   646
    if(status < 0) {
gabomdq@6354
   647
        LOGE("failed to attach current thread");
gabomdq@6354
   648
        return 0;
gabomdq@6354
   649
    }
gabomdq@6354
   650
gabomdq@6354
   651
    /* From http://developer.android.com/guide/practices/jni.html
gabomdq@6354
   652
     * Threads attached through JNI must call DetachCurrentThread before they exit. If coding this directly is awkward,
gabomdq@6354
   653
     * in Android 2.0 (Eclair) and higher you can use pthread_key_create to define a destructor function that will be
gabomdq@6354
   654
     * called before the thread exits, and call DetachCurrentThread from there. (Use that key with pthread_setspecific
gabomdq@6354
   655
     * to store the JNIEnv in thread-local-storage; that way it'll be passed into your destructor as the argument.)
gabomdq@6354
   656
     * Note: The destructor is not called unless the stored value is != NULL
gabomdq@6354
   657
     * Note: You can call this function any number of times for the same thread, there's no harm in it
gabomdq@6354
   658
     *       (except for some lost CPU cycles)
gabomdq@6354
   659
     */
gabomdq@6354
   660
    pthread_setspecific(mThreadKey, (void*) env);
slouken@8055
   661
slouken@8055
   662
    return env;
slouken@8055
   663
}
slouken@8055
   664
slouken@8055
   665
int Android_JNI_SetupThread(void)
slouken@8055
   666
{
slouken@8055
   667
    Android_JNI_GetEnv();
gabomdq@6354
   668
    return 1;
gabomdq@6354
   669
}
gabomdq@6354
   670
philipp@11061
   671
jclass Android_JNI_GetActivityClass(void)
philipp@11061
   672
{
philipp@11061
   673
    return mActivityClass;
philipp@11061
   674
}
philipp@11061
   675
gabomdq@7663
   676
/*
gabomdq@7663
   677
 * Audio support
gabomdq@7663
   678
 */
slouken@4996
   679
static jboolean audioBuffer16Bit = JNI_FALSE;
slouken@4996
   680
static jobject audioBuffer = NULL;
slouken@4996
   681
static void* audioBufferPinned = NULL;
icculus@10280
   682
static jboolean captureBuffer16Bit = JNI_FALSE;
icculus@10280
   683
static jobject captureBuffer = NULL;
slouken@4995
   684
icculus@10280
   685
int Android_JNI_OpenAudioDevice(int iscapture, int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
slouken@4964
   686
{
philipp@9322
   687
    jboolean audioBufferStereo;
slouken@4996
   688
    int audioBufferFrames;
icculus@10280
   689
    jobject jbufobj = NULL;
ewing@10080
   690
    jboolean isCopy;
slouken@4964
   691
gabomdq@6354
   692
    JNIEnv *env = Android_JNI_GetEnv();
gabomdq@6354
   693
gabomdq@6354
   694
    if (!env) {
gabomdq@6354
   695
        LOGE("callback_handler: failed to attach current thread");
icculus@5996
   696
    }
gabomdq@6354
   697
    Android_JNI_SetupThread();
icculus@5996
   698
slouken@4996
   699
    audioBufferStereo = channelCount > 1;
slouken@4964
   700
icculus@10280
   701
    if (iscapture) {
icculus@10280
   702
        __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device for capture");
icculus@10280
   703
        captureBuffer16Bit = is16Bit;
icculus@10280
   704
        if ((*env)->CallStaticIntMethod(env, mActivityClass, midCaptureOpen, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames) != 0) {
icculus@10280
   705
            /* Error during audio initialization */
icculus@10280
   706
            __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioRecord initialization!");
icculus@10280
   707
            return 0;
icculus@10280
   708
        }
icculus@10280
   709
    } else {
icculus@10280
   710
        __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device for output");
icculus@10280
   711
        audioBuffer16Bit = is16Bit;
icculus@10280
   712
        if ((*env)->CallStaticIntMethod(env, mActivityClass, midAudioOpen, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames) != 0) {
icculus@10280
   713
            /* Error during audio initialization */
icculus@10280
   714
            __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioTrack initialization!");
icculus@10280
   715
            return 0;
icculus@10280
   716
        }
gabomdq@7552
   717
    }
gabomdq@6802
   718
gabomdq@6802
   719
    /* Allocating the audio buffer from the Java side and passing it as the return value for audioInit no longer works on
gabomdq@6802
   720
     * Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */
slouken@7191
   721
gabomdq@6802
   722
    if (is16Bit) {
ewing@7501
   723
        jshortArray audioBufferLocal = (*env)->NewShortArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
gabomdq@6802
   724
        if (audioBufferLocal) {
icculus@10280
   725
            jbufobj = (*env)->NewGlobalRef(env, audioBufferLocal);
ewing@7501
   726
            (*env)->DeleteLocalRef(env, audioBufferLocal);
gabomdq@6802
   727
        }
gabomdq@6802
   728
    }
gabomdq@6802
   729
    else {
ewing@7501
   730
        jbyteArray audioBufferLocal = (*env)->NewByteArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
gabomdq@6802
   731
        if (audioBufferLocal) {
icculus@10280
   732
            jbufobj = (*env)->NewGlobalRef(env, audioBufferLocal);
ewing@7501
   733
            (*env)->DeleteLocalRef(env, audioBufferLocal);
gabomdq@6802
   734
        }
gabomdq@6802
   735
    }
slouken@4995
   736
icculus@10280
   737
    if (jbufobj == NULL) {
gabomdq@6802
   738
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: could not allocate an audio buffer!");
slouken@4996
   739
        return 0;
slouken@4996
   740
    }
slouken@4995
   741
icculus@10280
   742
    if (iscapture) {
icculus@10280
   743
        captureBuffer = jbufobj;
icculus@10280
   744
    } else {
icculus@10280
   745
        audioBuffer = jbufobj;
icculus@10280
   746
    }
icculus@10280
   747
ewing@10080
   748
    isCopy = JNI_FALSE;
icculus@10280
   749
icculus@10280
   750
    if (is16Bit) {
icculus@10280
   751
        if (!iscapture) {
icculus@10280
   752
            audioBufferPinned = (*env)->GetShortArrayElements(env, (jshortArray)audioBuffer, &isCopy);
icculus@10280
   753
        }
ewing@7501
   754
        audioBufferFrames = (*env)->GetArrayLength(env, (jshortArray)audioBuffer);
slouken@4996
   755
    } else {
icculus@10280
   756
        if (!iscapture) {
icculus@10280
   757
            audioBufferPinned = (*env)->GetByteArrayElements(env, (jbyteArray)audioBuffer, &isCopy);
icculus@10280
   758
        }
ewing@7501
   759
        audioBufferFrames = (*env)->GetArrayLength(env, (jbyteArray)audioBuffer);
slouken@4996
   760
    }
icculus@10280
   761
slouken@4996
   762
    if (audioBufferStereo) {
slouken@4996
   763
        audioBufferFrames /= 2;
slouken@4996
   764
    }
gabomdq@6864
   765
slouken@4996
   766
    return audioBufferFrames;
slouken@4995
   767
}
slouken@4995
   768
philipp@9594
   769
void * Android_JNI_GetAudioBuffer(void)
slouken@4995
   770
{
slouken@4996
   771
    return audioBufferPinned;
slouken@4995
   772
}
slouken@4995
   773
philipp@9594
   774
void Android_JNI_WriteAudioBuffer(void)
slouken@4995
   775
{
gabomdq@6354
   776
    JNIEnv *mAudioEnv = Android_JNI_GetEnv();
gabomdq@6354
   777
slouken@4996
   778
    if (audioBuffer16Bit) {
ewing@7501
   779
        (*mAudioEnv)->ReleaseShortArrayElements(mAudioEnv, (jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
ewing@7501
   780
        (*mAudioEnv)->CallStaticVoidMethod(mAudioEnv, mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
slouken@4996
   781
    } else {
ewing@7501
   782
        (*mAudioEnv)->ReleaseByteArrayElements(mAudioEnv, (jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
ewing@7501
   783
        (*mAudioEnv)->CallStaticVoidMethod(mAudioEnv, mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
slouken@4996
   784
    }
slouken@4995
   785
slouken@4996
   786
    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
slouken@4995
   787
}
slouken@4995
   788
icculus@10280
   789
int Android_JNI_CaptureAudioBuffer(void *buffer, int buflen)
icculus@10280
   790
{
icculus@10280
   791
    JNIEnv *env = Android_JNI_GetEnv();
icculus@10280
   792
    jboolean isCopy = JNI_FALSE;
icculus@10280
   793
    jint br;
icculus@10280
   794
icculus@10280
   795
    if (captureBuffer16Bit) {
icculus@10280
   796
        SDL_assert((*env)->GetArrayLength(env, (jshortArray)captureBuffer) == (buflen / 2));
icculus@10280
   797
        br = (*env)->CallStaticIntMethod(env, mActivityClass, midCaptureReadShortBuffer, (jshortArray)captureBuffer, JNI_TRUE);
icculus@10280
   798
        if (br > 0) {
icculus@10280
   799
            jshort *ptr = (*env)->GetShortArrayElements(env, (jshortArray)captureBuffer, &isCopy);
icculus@10280
   800
            br *= 2;
icculus@10280
   801
            SDL_memcpy(buffer, ptr, br);
icculus@10280
   802
            (*env)->ReleaseShortArrayElements(env, (jshortArray)captureBuffer, (jshort *)ptr, JNI_ABORT);
icculus@10280
   803
        }
icculus@10280
   804
    } else {
icculus@10280
   805
        SDL_assert((*env)->GetArrayLength(env, (jshortArray)captureBuffer) == buflen);
icculus@10280
   806
        br = (*env)->CallStaticIntMethod(env, mActivityClass, midCaptureReadByteBuffer, (jbyteArray)captureBuffer, JNI_TRUE);
icculus@10280
   807
        if (br > 0) {
icculus@10280
   808
            jbyte *ptr = (*env)->GetByteArrayElements(env, (jbyteArray)captureBuffer, &isCopy);
icculus@10280
   809
            SDL_memcpy(buffer, ptr, br);
icculus@10280
   810
            (*env)->ReleaseByteArrayElements(env, (jbyteArray)captureBuffer, (jbyte *)ptr, JNI_ABORT);
icculus@10280
   811
        }
icculus@10280
   812
    }
icculus@10280
   813
icculus@10280
   814
    return (int) br;
icculus@10280
   815
}
icculus@10280
   816
icculus@10280
   817
void Android_JNI_FlushCapturedAudio(void)
icculus@10280
   818
{
icculus@10280
   819
    JNIEnv *env = Android_JNI_GetEnv();
icculus@10280
   820
    #if 0  /* !!! FIXME: this needs API 23, or it'll do blocking reads and never end. */
icculus@10280
   821
    if (captureBuffer16Bit) {
icculus@10280
   822
        const jint len = (*env)->GetArrayLength(env, (jshortArray)captureBuffer);
icculus@10280
   823
        while ((*env)->CallStaticIntMethod(env, mActivityClass, midCaptureReadShortBuffer, (jshortArray)captureBuffer, JNI_FALSE) == len) { /* spin */ }
icculus@10280
   824
    } else {
icculus@10280
   825
        const jint len = (*env)->GetArrayLength(env, (jbyteArray)captureBuffer);
icculus@10280
   826
        while ((*env)->CallStaticIntMethod(env, mActivityClass, midCaptureReadByteBuffer, (jbyteArray)captureBuffer, JNI_FALSE) == len) { /* spin */ }
icculus@10280
   827
    }
icculus@10280
   828
    #else
icculus@10280
   829
    if (captureBuffer16Bit) {
icculus@10280
   830
        (*env)->CallStaticIntMethod(env, mActivityClass, midCaptureReadShortBuffer, (jshortArray)captureBuffer, JNI_FALSE);
icculus@10280
   831
    } else {
icculus@10280
   832
        (*env)->CallStaticIntMethod(env, mActivityClass, midCaptureReadByteBuffer, (jbyteArray)captureBuffer, JNI_FALSE);
icculus@10280
   833
    }
icculus@10280
   834
    #endif
icculus@10280
   835
}
icculus@10280
   836
icculus@10280
   837
void Android_JNI_CloseAudioDevice(const int iscapture)
slouken@4995
   838
{
gabomdq@6354
   839
    JNIEnv *env = Android_JNI_GetEnv();
icculus@5996
   840
icculus@10280
   841
    if (iscapture) {
icculus@10280
   842
        (*env)->CallStaticVoidMethod(env, mActivityClass, midCaptureClose);
icculus@10280
   843
        if (captureBuffer) {
icculus@10280
   844
            (*env)->DeleteGlobalRef(env, captureBuffer);
icculus@10280
   845
            captureBuffer = NULL;
icculus@10280
   846
        }
icculus@10280
   847
    } else {
icculus@10280
   848
        (*env)->CallStaticVoidMethod(env, mActivityClass, midAudioClose);
icculus@10280
   849
        if (audioBuffer) {
icculus@10280
   850
            (*env)->DeleteGlobalRef(env, audioBuffer);
icculus@10280
   851
            audioBuffer = NULL;
icculus@10280
   852
            audioBufferPinned = NULL;
icculus@10280
   853
        }
slouken@4997
   854
    }
slouken@4964
   855
}
slouken@4981
   856
gabomdq@7663
   857
/* Test for an exception and call SDL_SetError with its detail if one occurs */
gabomdq@7663
   858
/* If the parameter silent is truthy then SDL_SetError() will not be called. */
philipp@9271
   859
static SDL_bool Android_JNI_ExceptionOccurred(SDL_bool silent)
tim@5650
   860
{
ewing@10080
   861
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@10080
   862
    jthrowable exception;
ewing@10080
   863
ewing@7501
   864
    SDL_assert(LocalReferenceHolder_IsActive());
slouken@6284
   865
ewing@10080
   866
    exception = (*mEnv)->ExceptionOccurred(mEnv);
tim@5650
   867
    if (exception != NULL) {
tim@5650
   868
        jmethodID mid;
tim@5650
   869
gabomdq@7663
   870
        /* Until this happens most JNI operations have undefined behaviour */
ewing@7501
   871
        (*mEnv)->ExceptionClear(mEnv);
tim@5650
   872
slouken@7045
   873
        if (!silent) {
ewing@7501
   874
            jclass exceptionClass = (*mEnv)->GetObjectClass(mEnv, exception);
ewing@7501
   875
            jclass classClass = (*mEnv)->FindClass(mEnv, "java/lang/Class");
ewing@10080
   876
            jstring exceptionName;
ewing@10080
   877
            const char* exceptionNameUTF8;
ewing@10080
   878
            jstring exceptionMessage;
tim@5650
   879
ewing@7501
   880
            mid = (*mEnv)->GetMethodID(mEnv, classClass, "getName", "()Ljava/lang/String;");
ewing@10080
   881
            exceptionName = (jstring)(*mEnv)->CallObjectMethod(mEnv, exceptionClass, mid);
ewing@10080
   882
            exceptionNameUTF8 = (*mEnv)->GetStringUTFChars(mEnv, exceptionName, 0);
tim@5650
   883
ewing@7501
   884
            mid = (*mEnv)->GetMethodID(mEnv, exceptionClass, "getMessage", "()Ljava/lang/String;");
ewing@10080
   885
            exceptionMessage = (jstring)(*mEnv)->CallObjectMethod(mEnv, exception, mid);
tim@5650
   886
slouken@7045
   887
            if (exceptionMessage != NULL) {
ewing@7501
   888
                const char* exceptionMessageUTF8 = (*mEnv)->GetStringUTFChars(mEnv, exceptionMessage, 0);
slouken@7045
   889
                SDL_SetError("%s: %s", exceptionNameUTF8, exceptionMessageUTF8);
ewing@7501
   890
                (*mEnv)->ReleaseStringUTFChars(mEnv, exceptionMessage, exceptionMessageUTF8);
slouken@7045
   891
            } else {
slouken@7045
   892
                SDL_SetError("%s", exceptionNameUTF8);
slouken@7045
   893
            }
slouken@7045
   894
ewing@7501
   895
            (*mEnv)->ReleaseStringUTFChars(mEnv, exceptionName, exceptionNameUTF8);
tim@5650
   896
        }
tim@5650
   897
philipp@9271
   898
        return SDL_TRUE;
tim@5650
   899
    }
tim@5650
   900
philipp@9271
   901
    return SDL_FALSE;
tim@5650
   902
}
tim@5650
   903
ewing@7501
   904
static int Internal_Android_JNI_FileOpen(SDL_RWops* ctx)
icculus@5582
   905
{
ewing@7501
   906
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
ewing@7501
   907
tim@5650
   908
    int result = 0;
tim@5650
   909
tim@5650
   910
    jmethodID mid;
tim@5650
   911
    jobject context;
tim@5650
   912
    jobject assetManager;
tim@5650
   913
    jobject inputStream;
tim@5650
   914
    jclass channels;
tim@5650
   915
    jobject readableByteChannel;
tim@5650
   916
    jstring fileNameJString;
gabomdq@6806
   917
    jobject fd;
gabomdq@6806
   918
    jclass fdCls;
gabomdq@6806
   919
    jfieldID descriptor;
tim@5650
   920
gabomdq@6354
   921
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
   922
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
tim@5650
   923
        goto failure;
tim@5650
   924
    }
tim@5650
   925
gabomdq@6308
   926
    fileNameJString = (jstring)ctx->hidden.androidio.fileNameRef;
gabomdq@6806
   927
    ctx->hidden.androidio.position = 0;
icculus@5582
   928
gabomdq@7663
   929
    /* context = SDLActivity.getContext(); */
ewing@7501
   930
    mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
icculus@5582
   931
            "getContext","()Landroid/content/Context;");
ewing@7501
   932
    context = (*mEnv)->CallStaticObjectMethod(mEnv, mActivityClass, mid);
slouken@7191
   933
icculus@5582
   934
gabomdq@7663
   935
    /* assetManager = context.getAssets(); */
ewing@7501
   936
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, context),
tim@5650
   937
            "getAssets", "()Landroid/content/res/AssetManager;");
ewing@7501
   938
    assetManager = (*mEnv)->CallObjectMethod(mEnv, context, mid);
icculus@5582
   939
gabomdq@6806
   940
    /* First let's try opening the file to obtain an AssetFileDescriptor.
gabomdq@6806
   941
    * This method reads the files directly from the APKs using standard *nix calls
gabomdq@6806
   942
    */
ewing@7501
   943
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, assetManager), "openFd", "(Ljava/lang/String;)Landroid/content/res/AssetFileDescriptor;");
ewing@7501
   944
    inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString);
philipp@9271
   945
    if (Android_JNI_ExceptionOccurred(SDL_TRUE)) {
gabomdq@6806
   946
        goto fallback;
icculus@5582
   947
    }
icculus@5582
   948
ewing@7501
   949
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getStartOffset", "()J");
ewing@7501
   950
    ctx->hidden.androidio.offset = (*mEnv)->CallLongMethod(mEnv, inputStream, mid);
philipp@9271
   951
    if (Android_JNI_ExceptionOccurred(SDL_TRUE)) {
gabomdq@6806
   952
        goto fallback;
icculus@5582
   953
    }
icculus@5582
   954
ewing@7501
   955
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getDeclaredLength", "()J");
ewing@7501
   956
    ctx->hidden.androidio.size = (*mEnv)->CallLongMethod(mEnv, inputStream, mid);
philipp@9271
   957
    if (Android_JNI_ExceptionOccurred(SDL_TRUE)) {
gabomdq@6806
   958
        goto fallback;
icculus@5582
   959
    }
icculus@5582
   960
ewing@7501
   961
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getFileDescriptor", "()Ljava/io/FileDescriptor;");
ewing@7501
   962
    fd = (*mEnv)->CallObjectMethod(mEnv, inputStream, mid);
ewing@7501
   963
    fdCls = (*mEnv)->GetObjectClass(mEnv, fd);
ewing@7501
   964
    descriptor = (*mEnv)->GetFieldID(mEnv, fdCls, "descriptor", "I");
ewing@7501
   965
    ctx->hidden.androidio.fd = (*mEnv)->GetIntField(mEnv, fd, descriptor);
ewing@7501
   966
    ctx->hidden.androidio.assetFileDescriptorRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);
tim@5650
   967
gabomdq@7663
   968
    /* Seek to the correct offset in the file. */
gabomdq@6816
   969
    lseek(ctx->hidden.androidio.fd, (off_t)ctx->hidden.androidio.offset, SEEK_SET);
gabomdq@6816
   970
philipp@9271
   971
    if (0) {
gabomdq@6806
   972
fallback:
gabomdq@7663
   973
        /* Disabled log message because of spam on the Nexus 7 */
gabomdq@7678
   974
        /* __android_log_print(ANDROID_LOG_DEBUG, "SDL", "Falling back to legacy InputStream method for opening file"); */
slouken@7190
   975
gabomdq@6806
   976
        /* Try the old method using InputStream */
gabomdq@6806
   977
        ctx->hidden.androidio.assetFileDescriptorRef = NULL;
icculus@5582
   978
gabomdq@7663
   979
        /* inputStream = assetManager.open(<filename>); */
ewing@7501
   980
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, assetManager),
gabomdq@6806
   981
                "open", "(Ljava/lang/String;I)Ljava/io/InputStream;");
gabomdq@7678
   982
        inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString, 1 /* ACCESS_RANDOM */);
philipp@9271
   983
        if (Android_JNI_ExceptionOccurred(SDL_FALSE)) {
philipp@9869
   984
            /* Try fallback to APK expansion files */
alexey@8896
   985
            mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, context),
philipp@9869
   986
                "openAPKExpansionInputStream", "(Ljava/lang/String;)Ljava/io/InputStream;");
philipp@9873
   987
            if (!mid) {
philipp@9873
   988
                SDL_SetError("No openAPKExpansionInputStream() in Java class");
philipp@9873
   989
                goto failure; /* Java class is missing the required method */
philipp@9873
   990
            }
alexey@8896
   991
            inputStream = (*mEnv)->CallObjectMethod(mEnv, context, mid, fileNameJString);
alexey@8896
   992
philipp@9870
   993
            /* Exception is checked first because it always needs to be cleared.
philipp@9870
   994
             * If no exception occurred then the last SDL error message is kept.
philipp@9870
   995
             */
philipp@9870
   996
            if (Android_JNI_ExceptionOccurred(SDL_FALSE) || !inputStream) {
alexey@8896
   997
                goto failure;
alexey@8896
   998
            }
gabomdq@6806
   999
        }
gabomdq@6806
  1000
ewing@7501
  1001
        ctx->hidden.androidio.inputStreamRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);
gabomdq@6806
  1002
gabomdq@7663
  1003
        /* Despite all the visible documentation on [Asset]InputStream claiming
gabomdq@7663
  1004
         * that the .available() method is not guaranteed to return the entire file
gabomdq@7663
  1005
         * size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
gabomdq@7663
  1006
         * android/apis/content/ReadAsset.java imply that Android's
gabomdq@7663
  1007
         * AssetInputStream.available() /will/ always return the total file size
gabomdq@7663
  1008
        */
gabomdq@7663
  1009
        
gabomdq@7663
  1010
        /* size = inputStream.available(); */
ewing@7501
  1011
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
gabomdq@6806
  1012
                "available", "()I");
ewing@7501
  1013
        ctx->hidden.androidio.size = (long)(*mEnv)->CallIntMethod(mEnv, inputStream, mid);
philipp@9271
  1014
        if (Android_JNI_ExceptionOccurred(SDL_FALSE)) {
gabomdq@6806
  1015
            goto failure;
gabomdq@6806
  1016
        }
gabomdq@6806
  1017
gabomdq@7663
  1018
        /* readableByteChannel = Channels.newChannel(inputStream); */
ewing@7501
  1019
        channels = (*mEnv)->FindClass(mEnv, "java/nio/channels/Channels");
ewing@7501
  1020
        mid = (*mEnv)->GetStaticMethodID(mEnv, channels,
gabomdq@6806
  1021
                "newChannel",
gabomdq@6806
  1022
                "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
ewing@7501
  1023
        readableByteChannel = (*mEnv)->CallStaticObjectMethod(
ewing@7501
  1024
                mEnv, channels, mid, inputStream);
philipp@9271
  1025
        if (Android_JNI_ExceptionOccurred(SDL_FALSE)) {
gabomdq@6806
  1026
            goto failure;
gabomdq@6806
  1027
        }
gabomdq@6806
  1028
gabomdq@6806
  1029
        ctx->hidden.androidio.readableByteChannelRef =
ewing@7501
  1030
            (*mEnv)->NewGlobalRef(mEnv, readableByteChannel);
gabomdq@6806
  1031
gabomdq@7663
  1032
        /* Store .read id for reading purposes */
ewing@7501
  1033
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, readableByteChannel),
gabomdq@6806
  1034
                "read", "(Ljava/nio/ByteBuffer;)I");
gabomdq@6806
  1035
        ctx->hidden.androidio.readMethod = mid;
gabomdq@6806
  1036
    }
icculus@5582
  1037
philipp@9271
  1038
    if (0) {
tim@5650
  1039
failure:
tim@5650
  1040
        result = -1;
tim@5650
  1041
ewing@7501
  1042
        (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.fileNameRef);
tim@5650
  1043
tim@5650
  1044
        if(ctx->hidden.androidio.inputStreamRef != NULL) {
ewing@7501
  1045
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.inputStreamRef);
tim@5650
  1046
        }
gabomdq@6308
  1047
gabomdq@6308
  1048
        if(ctx->hidden.androidio.readableByteChannelRef != NULL) {
ewing@7501
  1049
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.readableByteChannelRef);
gabomdq@6308
  1050
        }
gabomdq@6308
  1051
gabomdq@6806
  1052
        if(ctx->hidden.androidio.assetFileDescriptorRef != NULL) {
ewing@7501
  1053
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.assetFileDescriptorRef);
gabomdq@6806
  1054
        }
gabomdq@6806
  1055
tim@5650
  1056
    }
ewing@7501
  1057
    
ewing@7501
  1058
    LocalReferenceHolder_Cleanup(&refs);
tim@5650
  1059
    return result;
icculus@5582
  1060
}
icculus@5582
  1061
ewing@7501
  1062
int Android_JNI_FileOpen(SDL_RWops* ctx,
ewing@7501
  1063
        const char* fileName, const char* mode)
icculus@5582
  1064
{
ewing@7501
  1065
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
gabomdq@6354
  1066
    JNIEnv *mEnv = Android_JNI_GetEnv();
ewing@7501
  1067
    int retval;
ewing@10080
  1068
    jstring fileNameJString;
slouken@6284
  1069
ewing@7501
  1070
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
ewing@7501
  1071
        LocalReferenceHolder_Cleanup(&refs);        
slouken@6284
  1072
        return -1;
slouken@6284
  1073
    }
slouken@6284
  1074
icculus@5582
  1075
    if (!ctx) {
ewing@7501
  1076
        LocalReferenceHolder_Cleanup(&refs);
icculus@5582
  1077
        return -1;
icculus@5582
  1078
    }
icculus@5582
  1079
ewing@10080
  1080
    fileNameJString = (*mEnv)->NewStringUTF(mEnv, fileName);
ewing@7501
  1081
    ctx->hidden.androidio.fileNameRef = (*mEnv)->NewGlobalRef(mEnv, fileNameJString);
tim@5650
  1082
    ctx->hidden.androidio.inputStreamRef = NULL;
gabomdq@6335
  1083
    ctx->hidden.androidio.readableByteChannelRef = NULL;
gabomdq@6335
  1084
    ctx->hidden.androidio.readMethod = NULL;
gabomdq@6806
  1085
    ctx->hidden.androidio.assetFileDescriptorRef = NULL;
icculus@5582
  1086
ewing@7501
  1087
    retval = Internal_Android_JNI_FileOpen(ctx);
ewing@7501
  1088
    LocalReferenceHolder_Cleanup(&refs);
ewing@7501
  1089
    return retval;
icculus@5582
  1090
}
icculus@5582
  1091
ewing@7501
  1092
size_t Android_JNI_FileRead(SDL_RWops* ctx, void* buffer,
icculus@5582
  1093
        size_t size, size_t maxnum)
icculus@5582
  1094
{
ewing@7501
  1095
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
icculus@5582
  1096
gabomdq@6806
  1097
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
gabomdq@6806
  1098
        size_t bytesMax = size * maxnum;
ewing@10080
  1099
        size_t result;
gabomdq@7678
  1100
        if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && ctx->hidden.androidio.position + bytesMax > ctx->hidden.androidio.size) {
gabomdq@6806
  1101
            bytesMax = ctx->hidden.androidio.size - ctx->hidden.androidio.position;
gabomdq@6806
  1102
        }
ewing@10080
  1103
        result = read(ctx->hidden.androidio.fd, buffer, bytesMax );
gabomdq@6806
  1104
        if (result > 0) {
gabomdq@6806
  1105
            ctx->hidden.androidio.position += result;
ewing@7501
  1106
            LocalReferenceHolder_Cleanup(&refs);
gabomdq@6806
  1107
            return result / size;
gabomdq@6806
  1108
        }
ewing@7501
  1109
        LocalReferenceHolder_Cleanup(&refs);
gabomdq@6806
  1110
        return 0;
gabomdq@6806
  1111
    } else {
gabomdq@6806
  1112
        jlong bytesRemaining = (jlong) (size * maxnum);
gabomdq@6806
  1113
        jlong bytesMax = (jlong) (ctx->hidden.androidio.size -  ctx->hidden.androidio.position);
gabomdq@6806
  1114
        int bytesRead = 0;
ewing@10080
  1115
        JNIEnv *mEnv;
ewing@10080
  1116
        jobject readableByteChannel;
ewing@10080
  1117
        jmethodID readMethod;
ewing@10080
  1118
        jobject byteBuffer;
gabomdq@6377
  1119
gabomdq@6806
  1120
        /* Don't read more bytes than those that remain in the file, otherwise we get an exception */
gabomdq@6806
  1121
        if (bytesRemaining >  bytesMax) bytesRemaining = bytesMax;
slouken@6284
  1122
ewing@10080
  1123
        mEnv = Android_JNI_GetEnv();
ewing@7501
  1124
        if (!LocalReferenceHolder_Init(&refs, mEnv)) {
ewing@7501
  1125
            LocalReferenceHolder_Cleanup(&refs);            
philipp@7368
  1126
            return 0;
icculus@5582
  1127
        }
icculus@5582
  1128
ewing@10080
  1129
        readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannelRef;
ewing@10080
  1130
        readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
ewing@10080
  1131
        byteBuffer = (*mEnv)->NewDirectByteBuffer(mEnv, buffer, bytesRemaining);
gabomdq@6806
  1132
gabomdq@6806
  1133
        while (bytesRemaining > 0) {
gabomdq@7663
  1134
            /* result = readableByteChannel.read(...); */
ewing@7501
  1135
            int result = (*mEnv)->CallIntMethod(mEnv, readableByteChannel, readMethod, byteBuffer);
gabomdq@6806
  1136
philipp@9271
  1137
            if (Android_JNI_ExceptionOccurred(SDL_FALSE)) {
ewing@7501
  1138
                LocalReferenceHolder_Cleanup(&refs);            
gabomdq@6806
  1139
                return 0;
gabomdq@6806
  1140
            }
gabomdq@6806
  1141
gabomdq@6806
  1142
            if (result < 0) {
gabomdq@6806
  1143
                break;
gabomdq@6806
  1144
            }
gabomdq@6806
  1145
gabomdq@6806
  1146
            bytesRemaining -= result;
gabomdq@6806
  1147
            bytesRead += result;
gabomdq@6806
  1148
            ctx->hidden.androidio.position += result;
icculus@5582
  1149
        }
ewing@7501
  1150
        LocalReferenceHolder_Cleanup(&refs);                    
gabomdq@6806
  1151
        return bytesRead / size;
slouken@7191
  1152
    }
icculus@5582
  1153
}
icculus@5582
  1154
ewing@7501
  1155
size_t Android_JNI_FileWrite(SDL_RWops* ctx, const void* buffer,
icculus@5582
  1156
        size_t size, size_t num)
icculus@5582
  1157
{
icculus@5582
  1158
    SDL_SetError("Cannot write to Android package filesystem");
icculus@5582
  1159
    return 0;
icculus@5582
  1160
}
icculus@5582
  1161
philipp@9271
  1162
static int Internal_Android_JNI_FileClose(SDL_RWops* ctx, SDL_bool release)
icculus@5582
  1163
{
ewing@7501
  1164
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
ewing@7501
  1165
icculus@5582
  1166
    int result = 0;
gabomdq@6354
  1167
    JNIEnv *mEnv = Android_JNI_GetEnv();
icculus@5582
  1168
ewing@7501
  1169
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
ewing@7501
  1170
        LocalReferenceHolder_Cleanup(&refs);
icculus@7037
  1171
        return SDL_SetError("Failed to allocate enough JVM local references");
slouken@6284
  1172
    }
slouken@6284
  1173
icculus@5582
  1174
    if (ctx) {
icculus@5582
  1175
        if (release) {
ewing@7501
  1176
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.fileNameRef);
icculus@5582
  1177
        }
icculus@5582
  1178
gabomdq@6806
  1179
        if (ctx->hidden.androidio.assetFileDescriptorRef) {
gabomdq@6806
  1180
            jobject inputStream = (jobject)ctx->hidden.androidio.assetFileDescriptorRef;
ewing@7501
  1181
            jmethodID mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
gabomdq@6806
  1182
                    "close", "()V");
ewing@7501
  1183
            (*mEnv)->CallVoidMethod(mEnv, inputStream, mid);
ewing@7501
  1184
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.assetFileDescriptorRef);
philipp@9271
  1185
            if (Android_JNI_ExceptionOccurred(SDL_FALSE)) {
gabomdq@6806
  1186
                result = -1;
gabomdq@6806
  1187
            }
gabomdq@6806
  1188
        }
gabomdq@6806
  1189
        else {
gabomdq@6806
  1190
            jobject inputStream = (jobject)ctx->hidden.androidio.inputStreamRef;
icculus@5582
  1191
gabomdq@7677
  1192
            /* inputStream.close(); */
ewing@7501
  1193
            jmethodID mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
gabomdq@6806
  1194
                    "close", "()V");
ewing@7501
  1195
            (*mEnv)->CallVoidMethod(mEnv, inputStream, mid);
ewing@7501
  1196
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.inputStreamRef);
ewing@7501
  1197
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.readableByteChannelRef);
philipp@9271
  1198
            if (Android_JNI_ExceptionOccurred(SDL_FALSE)) {
gabomdq@6806
  1199
                result = -1;
gabomdq@6806
  1200
            }
icculus@5582
  1201
        }
icculus@5582
  1202
icculus@5582
  1203
        if (release) {
icculus@5582
  1204
            SDL_FreeRW(ctx);
icculus@5582
  1205
        }
icculus@5582
  1206
    }
icculus@5582
  1207
ewing@7501
  1208
    LocalReferenceHolder_Cleanup(&refs);
icculus@5582
  1209
    return result;
icculus@5582
  1210
}
icculus@5582
  1211
icculus@5582
  1212
ewing@7501
  1213
Sint64 Android_JNI_FileSize(SDL_RWops* ctx)
icculus@5582
  1214
{
slouken@6642
  1215
    return ctx->hidden.androidio.size;
slouken@6642
  1216
}
slouken@6642
  1217
ewing@7501
  1218
Sint64 Android_JNI_FileSeek(SDL_RWops* ctx, Sint64 offset, int whence)
slouken@6642
  1219
{
gabomdq@6806
  1220
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
ewing@10080
  1221
        off_t ret;
gabomdq@6806
  1222
        switch (whence) {
gabomdq@6806
  1223
            case RW_SEEK_SET:
gabomdq@7678
  1224
                if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && offset > ctx->hidden.androidio.size) offset = ctx->hidden.androidio.size;
gabomdq@6806
  1225
                offset += ctx->hidden.androidio.offset;
gabomdq@6806
  1226
                break;
gabomdq@6806
  1227
            case RW_SEEK_CUR:
gabomdq@6806
  1228
                offset += ctx->hidden.androidio.position;
gabomdq@7678
  1229
                if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && offset > ctx->hidden.androidio.size) offset = ctx->hidden.androidio.size;
gabomdq@6806
  1230
                offset += ctx->hidden.androidio.offset;
gabomdq@6806
  1231
                break;
gabomdq@6806
  1232
            case RW_SEEK_END:
gabomdq@6806
  1233
                offset = ctx->hidden.androidio.offset + ctx->hidden.androidio.size + offset;
gabomdq@6806
  1234
                break;
gabomdq@6806
  1235
            default:
icculus@7037
  1236
                return SDL_SetError("Unknown value for 'whence'");
gabomdq@6806
  1237
        }
gabomdq@6806
  1238
        whence = SEEK_SET;
icculus@5582
  1239
ewing@10080
  1240
        ret = lseek(ctx->hidden.androidio.fd, (off_t)offset, SEEK_SET);
gabomdq@6806
  1241
        if (ret == -1) return -1;
gabomdq@6806
  1242
        ctx->hidden.androidio.position = ret - ctx->hidden.androidio.offset;
gabomdq@6806
  1243
    } else {
gabomdq@6806
  1244
        Sint64 newPosition;
ewing@10080
  1245
        Sint64 movement;
gabomdq@6806
  1246
gabomdq@6806
  1247
        switch (whence) {
gabomdq@6806
  1248
            case RW_SEEK_SET:
gabomdq@6806
  1249
                newPosition = offset;
gabomdq@6806
  1250
                break;
gabomdq@6806
  1251
            case RW_SEEK_CUR:
gabomdq@6806
  1252
                newPosition = ctx->hidden.androidio.position + offset;
gabomdq@6806
  1253
                break;
gabomdq@6806
  1254
            case RW_SEEK_END:
gabomdq@6806
  1255
                newPosition = ctx->hidden.androidio.size + offset;
gabomdq@6806
  1256
                break;
gabomdq@6806
  1257
            default:
icculus@7037
  1258
                return SDL_SetError("Unknown value for 'whence'");
gabomdq@6806
  1259
        }
gabomdq@6806
  1260
gabomdq@6806
  1261
        /* Validate the new position */
gabomdq@6806
  1262
        if (newPosition < 0) {
icculus@7037
  1263
            return SDL_Error(SDL_EFSEEK);
gabomdq@6806
  1264
        }
gabomdq@6806
  1265
        if (newPosition > ctx->hidden.androidio.size) {
gabomdq@6806
  1266
            newPosition = ctx->hidden.androidio.size;
gabomdq@6806
  1267
        }
slouken@6642
  1268
ewing@10080
  1269
        movement = newPosition - ctx->hidden.androidio.position;
gabomdq@6806
  1270
        if (movement > 0) {
gabomdq@6806
  1271
            unsigned char buffer[4096];
icculus@5582
  1272
gabomdq@7663
  1273
            /* The easy case where we're seeking forwards */
gabomdq@6806
  1274
            while (movement > 0) {
gabomdq@6806
  1275
                Sint64 amount = sizeof (buffer);
ewing@10080
  1276
                size_t result;
gabomdq@6806
  1277
                if (amount > movement) {
gabomdq@6806
  1278
                    amount = movement;
gabomdq@6806
  1279
                }
ewing@10080
  1280
                result = Android_JNI_FileRead(ctx, buffer, 1, amount);
gabomdq@6806
  1281
                if (result <= 0) {
gabomdq@7663
  1282
                    /* Failed to read/skip the required amount, so fail */
gabomdq@6806
  1283
                    return -1;
gabomdq@6806
  1284
                }
tim@5993
  1285
gabomdq@6806
  1286
                movement -= result;
icculus@5582
  1287
            }
tim@5993
  1288
gabomdq@6806
  1289
        } else if (movement < 0) {
gabomdq@7663
  1290
            /* We can't seek backwards so we have to reopen the file and seek */
gabomdq@7663
  1291
            /* forwards which obviously isn't very efficient */
philipp@9271
  1292
            Internal_Android_JNI_FileClose(ctx, SDL_FALSE);
ewing@7501
  1293
            Internal_Android_JNI_FileOpen(ctx);
gabomdq@6806
  1294
            Android_JNI_FileSeek(ctx, newPosition, RW_SEEK_SET);
icculus@5582
  1295
        }
icculus@5582
  1296
    }
icculus@5582
  1297
icculus@5582
  1298
    return ctx->hidden.androidio.position;
slouken@7191
  1299
icculus@5582
  1300
}
icculus@5582
  1301
ewing@7501
  1302
int Android_JNI_FileClose(SDL_RWops* ctx)
icculus@5582
  1303
{
philipp@9271
  1304
    return Internal_Android_JNI_FileClose(ctx, SDL_TRUE);
icculus@5582
  1305
}
icculus@5582
  1306
gabomdq@7663
  1307
/* returns a new global reference which needs to be released later */
slouken@6464
  1308
static jobject Android_JNI_GetSystemServiceObject(const char* name)
slouken@6464
  1309
{
ewing@7501
  1310
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6464
  1311
    JNIEnv* env = Android_JNI_GetEnv();
ewing@7501
  1312
    jobject retval = NULL;
ewing@10080
  1313
    jstring service;
ewing@10080
  1314
    jmethodID mid;
ewing@10080
  1315
    jobject context;
ewing@10080
  1316
    jobject manager;
ewing@7501
  1317
ewing@7501
  1318
    if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1319
        LocalReferenceHolder_Cleanup(&refs);
slouken@6464
  1320
        return NULL;
slouken@6464
  1321
    }
slouken@6464
  1322
ewing@10080
  1323
    service = (*env)->NewStringUTF(env, name);
slouken@6464
  1324
ewing@7501
  1325
    mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
ewing@10080
  1326
    context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6464
  1327
philipp@8029
  1328
    mid = (*env)->GetMethodID(env, mActivityClass, "getSystemServiceFromUiThread", "(Ljava/lang/String;)Ljava/lang/Object;");
ewing@10080
  1329
    manager = (*env)->CallObjectMethod(env, context, mid, service);
slouken@6464
  1330
ewing@7501
  1331
    (*env)->DeleteLocalRef(env, service);
slouken@6464
  1332
ewing@7501
  1333
    retval = manager ? (*env)->NewGlobalRef(env, manager) : NULL;
ewing@7501
  1334
    LocalReferenceHolder_Cleanup(&refs);
ewing@7501
  1335
    return retval;
slouken@6464
  1336
}
slouken@6464
  1337
slouken@6464
  1338
#define SETUP_CLIPBOARD(error) \
ewing@7501
  1339
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__); \
slouken@6464
  1340
    JNIEnv* env = Android_JNI_GetEnv(); \
ewing@10080
  1341
    jobject clipboard; \
ewing@7501
  1342
    if (!LocalReferenceHolder_Init(&refs, env)) { \
ewing@7501
  1343
        LocalReferenceHolder_Cleanup(&refs); \
slouken@6464
  1344
        return error; \
slouken@6464
  1345
    } \
ewing@10080
  1346
    clipboard = Android_JNI_GetSystemServiceObject("clipboard"); \
slouken@6464
  1347
    if (!clipboard) { \
ewing@7501
  1348
        LocalReferenceHolder_Cleanup(&refs); \
slouken@6464
  1349
        return error; \
slouken@6464
  1350
    }
slouken@6464
  1351
ewing@7501
  1352
#define CLEANUP_CLIPBOARD() \
ewing@7501
  1353
    LocalReferenceHolder_Cleanup(&refs);
ewing@7501
  1354
ewing@7501
  1355
int Android_JNI_SetClipboardText(const char* text)
slouken@6464
  1356
{
ewing@10080
  1357
    /* Watch out for C89 scoping rules because of the macro */
slouken@6464
  1358
    SETUP_CLIPBOARD(-1)
slouken@6464
  1359
ewing@10080
  1360
    /* Nest the following in a scope to avoid C89 declaration rules triggered by the macro */
ewing@10080
  1361
    {
ewing@10080
  1362
        jmethodID mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, clipboard), "setText", "(Ljava/lang/CharSequence;)V");
ewing@10080
  1363
        jstring string = (*env)->NewStringUTF(env, text);
ewing@10080
  1364
        (*env)->CallVoidMethod(env, clipboard, mid, string);
ewing@10080
  1365
        (*env)->DeleteGlobalRef(env, clipboard);
ewing@10080
  1366
        (*env)->DeleteLocalRef(env, string);
ewing@10080
  1367
    }
ewing@7501
  1368
    CLEANUP_CLIPBOARD();
ewing@7501
  1369
slouken@6464
  1370
    return 0;
slouken@6464
  1371
}
slouken@6464
  1372
philipp@9594
  1373
char* Android_JNI_GetClipboardText(void)
slouken@6464
  1374
{
ewing@10080
  1375
    /* Watch out for C89 scoping rules because of the macro */
slouken@6464
  1376
    SETUP_CLIPBOARD(SDL_strdup(""))
slouken@6464
  1377
ewing@10080
  1378
    /* Nest the following in a scope to avoid C89 declaration rules triggered by the macro */
ewing@10080
  1379
    {
ewing@10080
  1380
        jmethodID mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, clipboard), "getText", "()Ljava/lang/CharSequence;");
ewing@10080
  1381
        jobject sequence = (*env)->CallObjectMethod(env, clipboard, mid);
ewing@10080
  1382
        (*env)->DeleteGlobalRef(env, clipboard);
ewing@10080
  1383
        if (sequence) {
ewing@10080
  1384
            jstring string;
ewing@10080
  1385
            const char* utf;
ewing@10080
  1386
            mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, sequence), "toString", "()Ljava/lang/String;");
ewing@10080
  1387
            string = (jstring)((*env)->CallObjectMethod(env, sequence, mid));
ewing@10080
  1388
            utf = (*env)->GetStringUTFChars(env, string, 0);
ewing@10080
  1389
            if (utf) {
ewing@10080
  1390
                char* text = SDL_strdup(utf);
ewing@10080
  1391
                (*env)->ReleaseStringUTFChars(env, string, utf);
ewing@7501
  1392
ewing@10080
  1393
                CLEANUP_CLIPBOARD();
ewing@7501
  1394
ewing@10080
  1395
                return text;
ewing@10080
  1396
            }
slouken@6464
  1397
        }
slouken@6464
  1398
    }
ewing@7501
  1399
    CLEANUP_CLIPBOARD();    
ewing@7501
  1400
slouken@6464
  1401
    return SDL_strdup("");
slouken@6464
  1402
}
slouken@6464
  1403
philipp@9594
  1404
SDL_bool Android_JNI_HasClipboardText(void)
slouken@6464
  1405
{
ewing@10080
  1406
    jmethodID mid;
ewing@10080
  1407
    jboolean has;
ewing@10080
  1408
    /* Watch out for C89 scoping rules because of the macro */
slouken@6464
  1409
    SETUP_CLIPBOARD(SDL_FALSE)
slouken@6464
  1410
ewing@10080
  1411
    mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, clipboard), "hasText", "()Z");
ewing@10080
  1412
    has = (*env)->CallBooleanMethod(env, clipboard, mid);
ewing@7501
  1413
    (*env)->DeleteGlobalRef(env, clipboard);
ewing@7501
  1414
ewing@7501
  1415
    CLEANUP_CLIPBOARD();
ewing@7501
  1416
    
slouken@6464
  1417
    return has ? SDL_TRUE : SDL_FALSE;
slouken@6464
  1418
}
slouken@6464
  1419
slouken@6464
  1420
gabomdq@7663
  1421
/* returns 0 on success or -1 on error (others undefined then)
gabomdq@7663
  1422
 * returns truthy or falsy value in plugged, charged and battery
gabomdq@7663
  1423
 * returns the value in seconds and percent or -1 if not available
gabomdq@7663
  1424
 */
ewing@7501
  1425
int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent)
slouken@6448
  1426
{
ewing@7501
  1427
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6448
  1428
    JNIEnv* env = Android_JNI_GetEnv();
ewing@10080
  1429
    jmethodID mid;
ewing@10080
  1430
    jobject context;
ewing@10080
  1431
    jstring action;
ewing@10080
  1432
    jclass cls;
ewing@10080
  1433
    jobject filter;
ewing@10080
  1434
    jobject intent;
ewing@10080
  1435
    jstring iname;
ewing@10080
  1436
    jmethodID imid;
ewing@10080
  1437
    jstring bname;
ewing@10080
  1438
    jmethodID bmid;
ewing@7501
  1439
    if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1440
        LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1441
        return -1;
slouken@6448
  1442
    }
slouken@6448
  1443
slouken@6448
  1444
ewing@7501
  1445
    mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
ewing@10080
  1446
    context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6448
  1447
ewing@10080
  1448
    action = (*env)->NewStringUTF(env, "android.intent.action.BATTERY_CHANGED");
slouken@6448
  1449
ewing@10080
  1450
    cls = (*env)->FindClass(env, "android/content/IntentFilter");
slouken@6448
  1451
ewing@7501
  1452
    mid = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V");
ewing@10080
  1453
    filter = (*env)->NewObject(env, cls, mid, action);
slouken@6448
  1454
ewing@7501
  1455
    (*env)->DeleteLocalRef(env, action);
slouken@6448
  1456
ewing@7501
  1457
    mid = (*env)->GetMethodID(env, mActivityClass, "registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;");
ewing@10080
  1458
    intent = (*env)->CallObjectMethod(env, context, mid, NULL, filter);
slouken@6448
  1459
ewing@7501
  1460
    (*env)->DeleteLocalRef(env, filter);
slouken@6448
  1461
ewing@7501
  1462
    cls = (*env)->GetObjectClass(env, intent);
slouken@6448
  1463
ewing@10080
  1464
    imid = (*env)->GetMethodID(env, cls, "getIntExtra", "(Ljava/lang/String;I)I");
slouken@6448
  1465
ewing@10080
  1466
    /* Watch out for C89 scoping rules because of the macro */
slouken@6448
  1467
#define GET_INT_EXTRA(var, key) \
ewing@10080
  1468
    int var; \
ewing@7501
  1469
    iname = (*env)->NewStringUTF(env, key); \
ewing@10080
  1470
    var = (*env)->CallIntMethod(env, intent, imid, iname, -1); \
ewing@7501
  1471
    (*env)->DeleteLocalRef(env, iname);
slouken@6448
  1472
ewing@10080
  1473
    bmid = (*env)->GetMethodID(env, cls, "getBooleanExtra", "(Ljava/lang/String;Z)Z");
slouken@6448
  1474
ewing@10080
  1475
    /* Watch out for C89 scoping rules because of the macro */
slouken@6448
  1476
#define GET_BOOL_EXTRA(var, key) \
ewing@10080
  1477
    int var; \
ewing@7501
  1478
    bname = (*env)->NewStringUTF(env, key); \
ewing@10080
  1479
    var = (*env)->CallBooleanMethod(env, intent, bmid, bname, JNI_FALSE); \
ewing@7501
  1480
    (*env)->DeleteLocalRef(env, bname);
slouken@6448
  1481
slouken@6448
  1482
    if (plugged) {
ewing@10080
  1483
        /* Watch out for C89 scoping rules because of the macro */
gabomdq@7663
  1484
        GET_INT_EXTRA(plug, "plugged") /* == BatteryManager.EXTRA_PLUGGED (API 5) */
slouken@6448
  1485
        if (plug == -1) {
ewing@7501
  1486
            LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1487
            return -1;
slouken@6448
  1488
        }
gabomdq@7663
  1489
        /* 1 == BatteryManager.BATTERY_PLUGGED_AC */
gabomdq@7663
  1490
        /* 2 == BatteryManager.BATTERY_PLUGGED_USB */
slouken@6448
  1491
        *plugged = (0 < plug) ? 1 : 0;
slouken@6448
  1492
    }
slouken@6448
  1493
slouken@6448
  1494
    if (charged) {
ewing@10080
  1495
        /* Watch out for C89 scoping rules because of the macro */
gabomdq@7663
  1496
        GET_INT_EXTRA(status, "status") /* == BatteryManager.EXTRA_STATUS (API 5) */
slouken@6448
  1497
        if (status == -1) {
ewing@7501
  1498
            LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1499
            return -1;
slouken@6448
  1500
        }
gabomdq@7663
  1501
        /* 5 == BatteryManager.BATTERY_STATUS_FULL */
slouken@6448
  1502
        *charged = (status == 5) ? 1 : 0;
slouken@6448
  1503
    }
slouken@6448
  1504
slouken@6448
  1505
    if (battery) {
gabomdq@7663
  1506
        GET_BOOL_EXTRA(present, "present") /* == BatteryManager.EXTRA_PRESENT (API 5) */
slouken@6448
  1507
        *battery = present ? 1 : 0;
slouken@6448
  1508
    }
slouken@6448
  1509
slouken@6448
  1510
    if (seconds) {
gabomdq@7663
  1511
        *seconds = -1; /* not possible */
slouken@6448
  1512
    }
slouken@6448
  1513
slouken@6448
  1514
    if (percent) {
ewing@10080
  1515
        int level;
ewing@10080
  1516
        int scale;
ewing@10080
  1517
        
ewing@10080
  1518
        /* Watch out for C89 scoping rules because of the macro */
ewing@10080
  1519
        {
ewing@10080
  1520
            GET_INT_EXTRA(level_temp, "level") /* == BatteryManager.EXTRA_LEVEL (API 5) */
ewing@10080
  1521
            level = level_temp;
ewing@10080
  1522
        }
ewing@10080
  1523
        /* Watch out for C89 scoping rules because of the macro */
ewing@10080
  1524
        {
ewing@10080
  1525
            GET_INT_EXTRA(scale_temp, "scale") /* == BatteryManager.EXTRA_SCALE (API 5) */
ewing@10080
  1526
            scale = scale_temp;
ewing@10080
  1527
        }
ewing@10080
  1528
        
slouken@6448
  1529
        if ((level == -1) || (scale == -1)) {
ewing@7501
  1530
            LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1531
            return -1;
slouken@6448
  1532
        }
slouken@6448
  1533
        *percent = level * 100 / scale;
slouken@6448
  1534
    }
slouken@6448
  1535
ewing@7501
  1536
    (*env)->DeleteLocalRef(env, intent);
slouken@6448
  1537
ewing@7501
  1538
    LocalReferenceHolder_Cleanup(&refs);
slouken@6448
  1539
    return 0;
slouken@6448
  1540
}
slouken@6448
  1541
philipp@7786
  1542
/* returns number of found touch devices as return value and ids in parameter ids */
philipp@7786
  1543
int Android_JNI_GetTouchDeviceIds(int **ids) {
philipp@7786
  1544
    JNIEnv *env = Android_JNI_GetEnv();
philipp@7786
  1545
    jint sources = 4098; /* == InputDevice.SOURCE_TOUCHSCREEN */
philipp@7786
  1546
    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "inputGetInputDeviceIds", "(I)[I");
philipp@7786
  1547
    jintArray array = (jintArray) (*env)->CallStaticObjectMethod(env, mActivityClass, mid, sources);
philipp@7786
  1548
    int number = 0;
philipp@7786
  1549
    *ids = NULL;
philipp@7786
  1550
    if (array) {
philipp@7786
  1551
        number = (int) (*env)->GetArrayLength(env, array);
philipp@7786
  1552
        if (0 < number) {
philipp@7786
  1553
            jint* elements = (*env)->GetIntArrayElements(env, array, NULL);
philipp@7786
  1554
            if (elements) {
philipp@7786
  1555
                int i;
slouken@7863
  1556
                *ids = SDL_malloc(number * sizeof (**ids));
philipp@7786
  1557
                for (i = 0; i < number; ++i) { /* not assuming sizeof (jint) == sizeof (int) */
slouken@7863
  1558
                    (*ids)[i] = elements[i];
philipp@7786
  1559
                }
philipp@7786
  1560
                (*env)->ReleaseIntArrayElements(env, array, elements, JNI_ABORT);
philipp@7786
  1561
            }
philipp@7786
  1562
        }
philipp@7786
  1563
        (*env)->DeleteLocalRef(env, array);
philipp@7786
  1564
    }
philipp@7786
  1565
    return number;
philipp@7786
  1566
}
philipp@7786
  1567
philipp@9594
  1568
void Android_JNI_PollInputDevices(void)
gabomdq@7907
  1569
{
gabomdq@8057
  1570
    JNIEnv *env = Android_JNI_GetEnv();
gabomdq@8057
  1571
    (*env)->CallStaticVoidMethod(env, mActivityClass, midPollInputDevices);    
gabomdq@7907
  1572
}
gabomdq@7907
  1573
philipp@9174
  1574
/* See SDLActivity.java for constants. */
philipp@9174
  1575
#define COMMAND_SET_KEEP_SCREEN_ON    5
philipp@9174
  1576
gabomdq@7663
  1577
/* sends message to be handled on the UI event dispatch thread */
ewing@7501
  1578
int Android_JNI_SendMessage(int command, int param)
slouken@6392
  1579
{
slouken@6392
  1580
    JNIEnv *env = Android_JNI_GetEnv();
ewing@10080
  1581
    jmethodID mid;
ewing@10080
  1582
    jboolean success;
slouken@6392
  1583
    if (!env) {
slouken@6392
  1584
        return -1;
slouken@6392
  1585
    }
ewing@10080
  1586
    mid = (*env)->GetStaticMethodID(env, mActivityClass, "sendMessage", "(II)Z");
slouken@6392
  1587
    if (!mid) {
slouken@6392
  1588
        return -1;
slouken@6392
  1589
    }
ewing@10080
  1590
    success = (*env)->CallStaticBooleanMethod(env, mActivityClass, mid, command, param);
philipp@7149
  1591
    return success ? 0 : -1;
slouken@6392
  1592
}
slouken@6392
  1593
philipp@9174
  1594
void Android_JNI_SuspendScreenSaver(SDL_bool suspend)
philipp@9174
  1595
{
philipp@9174
  1596
    Android_JNI_SendMessage(COMMAND_SET_KEEP_SCREEN_ON, (suspend == SDL_FALSE) ? 0 : 1);
philipp@9174
  1597
}
philipp@9174
  1598
ewing@7501
  1599
void Android_JNI_ShowTextInput(SDL_Rect *inputRect)
slouken@6555
  1600
{
slouken@6555
  1601
    JNIEnv *env = Android_JNI_GetEnv();
ewing@10080
  1602
    jmethodID mid;
slouken@6555
  1603
    if (!env) {
slouken@6654
  1604
        return;
slouken@6555
  1605
    }
slouken@6555
  1606
ewing@10080
  1607
    mid = (*env)->GetStaticMethodID(env, mActivityClass, "showTextInput", "(IIII)Z");
slouken@6555
  1608
    if (!mid) {
slouken@6654
  1609
        return;
slouken@6555
  1610
    }
ewing@7501
  1611
    (*env)->CallStaticBooleanMethod(env, mActivityClass, mid,
slouken@6555
  1612
                               inputRect->x,
slouken@6555
  1613
                               inputRect->y,
slouken@6555
  1614
                               inputRect->w,
slouken@6555
  1615
                               inputRect->h );
slouken@6555
  1616
}
slouken@6555
  1617
philipp@9594
  1618
void Android_JNI_HideTextInput(void)
slouken@6555
  1619
{
gabomdq@7663
  1620
    /* has to match Activity constant */
slouken@6654
  1621
    const int COMMAND_TEXTEDIT_HIDE = 3;
slouken@6654
  1622
    Android_JNI_SendMessage(COMMAND_TEXTEDIT_HIDE, 0);
slouken@6654
  1623
}
slouken@6555
  1624
slouken@9135
  1625
int Android_JNI_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slouken@9135
  1626
{
slouken@9135
  1627
    JNIEnv *env;
philipp@9231
  1628
    jclass clazz;
slouken@9135
  1629
    jmethodID mid;
slouken@9135
  1630
    jobject context;
slouken@9135
  1631
    jstring title;
slouken@9135
  1632
    jstring message;
slouken@9135
  1633
    jintArray button_flags;
slouken@9135
  1634
    jintArray button_ids;
slouken@9135
  1635
    jobjectArray button_texts;
slouken@9135
  1636
    jintArray colors;
philipp@9231
  1637
    jobject text;
slouken@9135
  1638
    jint temp;
slouken@9135
  1639
    int i;
slouken@9135
  1640
slouken@9135
  1641
    env = Android_JNI_GetEnv();
slouken@9135
  1642
slouken@9135
  1643
    /* convert parameters */
slouken@9135
  1644
philipp@9231
  1645
    clazz = (*env)->FindClass(env, "java/lang/String");
philipp@9231
  1646
slouken@9135
  1647
    title = (*env)->NewStringUTF(env, messageboxdata->title);
slouken@9135
  1648
    message = (*env)->NewStringUTF(env, messageboxdata->message);
slouken@9135
  1649
slouken@9135
  1650
    button_flags = (*env)->NewIntArray(env, messageboxdata->numbuttons);
slouken@9135
  1651
    button_ids = (*env)->NewIntArray(env, messageboxdata->numbuttons);
slouken@9135
  1652
    button_texts = (*env)->NewObjectArray(env, messageboxdata->numbuttons,
philipp@9231
  1653
        clazz, NULL);
slouken@9135
  1654
    for (i = 0; i < messageboxdata->numbuttons; ++i) {
slouken@9135
  1655
        temp = messageboxdata->buttons[i].flags;
slouken@9135
  1656
        (*env)->SetIntArrayRegion(env, button_flags, i, 1, &temp);
slouken@9135
  1657
        temp = messageboxdata->buttons[i].buttonid;
slouken@9135
  1658
        (*env)->SetIntArrayRegion(env, button_ids, i, 1, &temp);
philipp@9231
  1659
        text = (*env)->NewStringUTF(env, messageboxdata->buttons[i].text);
philipp@9231
  1660
        (*env)->SetObjectArrayElement(env, button_texts, i, text);
philipp@9231
  1661
        (*env)->DeleteLocalRef(env, text);
slouken@9135
  1662
    }
slouken@9135
  1663
slouken@9135
  1664
    if (messageboxdata->colorScheme) {
slouken@9135
  1665
        colors = (*env)->NewIntArray(env, SDL_MESSAGEBOX_COLOR_MAX);
slouken@9135
  1666
        for (i = 0; i < SDL_MESSAGEBOX_COLOR_MAX; ++i) {
slouken@9135
  1667
            temp = (0xFF << 24) |
slouken@9135
  1668
                   (messageboxdata->colorScheme->colors[i].r << 16) |
slouken@9135
  1669
                   (messageboxdata->colorScheme->colors[i].g << 8) |
slouken@9135
  1670
                   (messageboxdata->colorScheme->colors[i].b << 0);
slouken@9135
  1671
            (*env)->SetIntArrayRegion(env, colors, i, 1, &temp);
slouken@9135
  1672
        }
slouken@9135
  1673
    } else {
slouken@9135
  1674
        colors = NULL;
slouken@9135
  1675
    }
slouken@9135
  1676
philipp@9231
  1677
    (*env)->DeleteLocalRef(env, clazz);
philipp@9231
  1678
slouken@9135
  1679
    /* call function */
slouken@9135
  1680
slouken@9135
  1681
    mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext","()Landroid/content/Context;");
slouken@9135
  1682
slouken@9135
  1683
    context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@9135
  1684
philipp@9231
  1685
    clazz = (*env)->GetObjectClass(env, context);
philipp@9231
  1686
philipp@9231
  1687
    mid = (*env)->GetMethodID(env, clazz,
slouken@9135
  1688
        "messageboxShowMessageBox", "(ILjava/lang/String;Ljava/lang/String;[I[I[Ljava/lang/String;[I)I");
slouken@9135
  1689
    *buttonid = (*env)->CallIntMethod(env, context, mid,
slouken@9135
  1690
        messageboxdata->flags,
slouken@9135
  1691
        title,
slouken@9135
  1692
        message,
slouken@9135
  1693
        button_flags,
slouken@9135
  1694
        button_ids,
slouken@9135
  1695
        button_texts,
slouken@9135
  1696
        colors);
slouken@9135
  1697
philipp@9231
  1698
    (*env)->DeleteLocalRef(env, context);
philipp@9231
  1699
    (*env)->DeleteLocalRef(env, clazz);
philipp@9231
  1700
slouken@9135
  1701
    /* delete parameters */
slouken@9135
  1702
slouken@9135
  1703
    (*env)->DeleteLocalRef(env, title);
slouken@9135
  1704
    (*env)->DeleteLocalRef(env, message);
slouken@9135
  1705
    (*env)->DeleteLocalRef(env, button_flags);
slouken@9135
  1706
    (*env)->DeleteLocalRef(env, button_ids);
slouken@9135
  1707
    (*env)->DeleteLocalRef(env, button_texts);
slouken@9135
  1708
    (*env)->DeleteLocalRef(env, colors);
slouken@9135
  1709
slouken@9135
  1710
    return 0;
slouken@9135
  1711
}
slouken@9135
  1712
gabomdq@7663
  1713
/*
slouken@6630
  1714
//////////////////////////////////////////////////////////////////////////////
slouken@6630
  1715
//
slouken@6630
  1716
// Functions exposed to SDL applications in SDL_system.h
gabomdq@7663
  1717
//////////////////////////////////////////////////////////////////////////////
gabomdq@7663
  1718
*/
slouken@6630
  1719
philipp@11031
  1720
void *SDL_AndroidGetJNIEnv(void)
slouken@6630
  1721
{
slouken@6630
  1722
    return Android_JNI_GetEnv();
slouken@6630
  1723
}
slouken@6630
  1724
philipp@11031
  1725
void *SDL_AndroidGetActivity(void)
slouken@6630
  1726
{
gabomdq@7095
  1727
    /* See SDL_system.h for caveats on using this function. */
slouken@7191
  1728
slouken@6630
  1729
    jmethodID mid;
slouken@6630
  1730
slouken@6630
  1731
    JNIEnv *env = Android_JNI_GetEnv();
gabomdq@7083
  1732
    if (!env) {
slouken@6630
  1733
        return NULL;
slouken@6630
  1734
    }
slouken@6630
  1735
gabomdq@7663
  1736
    /* return SDLActivity.getContext(); */
ewing@7501
  1737
    mid = (*env)->GetStaticMethodID(env, mActivityClass,
slouken@6630
  1738
            "getContext","()Landroid/content/Context;");
ewing@7501
  1739
    return (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6630
  1740
}
slouken@6630
  1741
philipp@11031
  1742
const char * SDL_AndroidGetInternalStoragePath(void)
slouken@6630
  1743
{
slouken@6630
  1744
    static char *s_AndroidInternalFilesPath = NULL;
slouken@6630
  1745
slouken@6630
  1746
    if (!s_AndroidInternalFilesPath) {
ewing@7501
  1747
        struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6630
  1748
        jmethodID mid;
slouken@6630
  1749
        jobject context;
slouken@6630
  1750
        jobject fileObject;
slouken@6630
  1751
        jstring pathString;
slouken@6630
  1752
        const char *path;
slouken@6630
  1753
slouken@6630
  1754
        JNIEnv *env = Android_JNI_GetEnv();
ewing@7501
  1755
        if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1756
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1757
            return NULL;
slouken@6630
  1758
        }
slouken@6630
  1759
gabomdq@7663
  1760
        /* context = SDLActivity.getContext(); */
ewing@7501
  1761
        mid = (*env)->GetStaticMethodID(env, mActivityClass,
slouken@6630
  1762
                "getContext","()Landroid/content/Context;");
ewing@7501
  1763
        context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6630
  1764
gabomdq@7663
  1765
        /* fileObj = context.getFilesDir(); */
ewing@7501
  1766
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, context),
slouken@6630
  1767
                "getFilesDir", "()Ljava/io/File;");
ewing@7501
  1768
        fileObject = (*env)->CallObjectMethod(env, context, mid);
slouken@6630
  1769
        if (!fileObject) {
slouken@6630
  1770
            SDL_SetError("Couldn't get internal directory");
ewing@7501
  1771
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1772
            return NULL;
slouken@6630
  1773
        }
slouken@6630
  1774
gabomdq@7663
  1775
        /* path = fileObject.getAbsolutePath(); */
ewing@7501
  1776
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, fileObject),
slouken@6630
  1777
                "getAbsolutePath", "()Ljava/lang/String;");
ewing@7501
  1778
        pathString = (jstring)(*env)->CallObjectMethod(env, fileObject, mid);
slouken@6630
  1779
ewing@7501
  1780
        path = (*env)->GetStringUTFChars(env, pathString, NULL);
slouken@6630
  1781
        s_AndroidInternalFilesPath = SDL_strdup(path);
ewing@7501
  1782
        (*env)->ReleaseStringUTFChars(env, pathString, path);
ewing@7501
  1783
ewing@7501
  1784
        LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1785
    }
slouken@6630
  1786
    return s_AndroidInternalFilesPath;
slouken@6630
  1787
}
slouken@6630
  1788
philipp@11031
  1789
int SDL_AndroidGetExternalStorageState(void)
slouken@6630
  1790
{
ewing@7501
  1791
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6630
  1792
    jmethodID mid;
slouken@6630
  1793
    jclass cls;
slouken@6630
  1794
    jstring stateString;
slouken@6630
  1795
    const char *state;
slouken@6630
  1796
    int stateFlags;
slouken@6630
  1797
slouken@6630
  1798
    JNIEnv *env = Android_JNI_GetEnv();
ewing@7501
  1799
    if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1800
        LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1801
        return 0;
slouken@6630
  1802
    }
slouken@6630
  1803
ewing@7501
  1804
    cls = (*env)->FindClass(env, "android/os/Environment");
ewing@7501
  1805
    mid = (*env)->GetStaticMethodID(env, cls,
slouken@6630
  1806
            "getExternalStorageState", "()Ljava/lang/String;");
ewing@7501
  1807
    stateString = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid);
slouken@6630
  1808
ewing@7501
  1809
    state = (*env)->GetStringUTFChars(env, stateString, NULL);
slouken@6630
  1810
gabomdq@7663
  1811
    /* Print an info message so people debugging know the storage state */
slouken@6630
  1812
    __android_log_print(ANDROID_LOG_INFO, "SDL", "external storage state: %s", state);
slouken@6630
  1813
slouken@6630
  1814
    if (SDL_strcmp(state, "mounted") == 0) {
slouken@6630
  1815
        stateFlags = SDL_ANDROID_EXTERNAL_STORAGE_READ |
slouken@6630
  1816
                     SDL_ANDROID_EXTERNAL_STORAGE_WRITE;
slouken@6630
  1817
    } else if (SDL_strcmp(state, "mounted_ro") == 0) {
slouken@6630
  1818
        stateFlags = SDL_ANDROID_EXTERNAL_STORAGE_READ;
slouken@6630
  1819
    } else {
slouken@6630
  1820
        stateFlags = 0;
slouken@6630
  1821
    }
ewing@7501
  1822
    (*env)->ReleaseStringUTFChars(env, stateString, state);
slouken@6630
  1823
ewing@7501
  1824
    LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1825
    return stateFlags;
slouken@6630
  1826
}
slouken@6630
  1827
philipp@11031
  1828
const char * SDL_AndroidGetExternalStoragePath(void)
slouken@6630
  1829
{
slouken@6630
  1830
    static char *s_AndroidExternalFilesPath = NULL;
slouken@6630
  1831
slouken@6630
  1832
    if (!s_AndroidExternalFilesPath) {
ewing@7501
  1833
        struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
slouken@6630
  1834
        jmethodID mid;
slouken@6630
  1835
        jobject context;
slouken@6630
  1836
        jobject fileObject;
slouken@6630
  1837
        jstring pathString;
slouken@6630
  1838
        const char *path;
slouken@6630
  1839
slouken@6630
  1840
        JNIEnv *env = Android_JNI_GetEnv();
ewing@7501
  1841
        if (!LocalReferenceHolder_Init(&refs, env)) {
ewing@7501
  1842
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1843
            return NULL;
slouken@6630
  1844
        }
slouken@6630
  1845
gabomdq@7663
  1846
        /* context = SDLActivity.getContext(); */
ewing@7501
  1847
        mid = (*env)->GetStaticMethodID(env, mActivityClass,
slouken@6630
  1848
                "getContext","()Landroid/content/Context;");
ewing@7501
  1849
        context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
slouken@6630
  1850
gabomdq@7663
  1851
        /* fileObj = context.getExternalFilesDir(); */
ewing@7501
  1852
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, context),
slouken@6630
  1853
                "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;");
ewing@7501
  1854
        fileObject = (*env)->CallObjectMethod(env, context, mid, NULL);
slouken@6630
  1855
        if (!fileObject) {
slouken@6630
  1856
            SDL_SetError("Couldn't get external directory");
ewing@7501
  1857
            LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1858
            return NULL;
slouken@6630
  1859
        }
slouken@6630
  1860
gabomdq@7663
  1861
        /* path = fileObject.getAbsolutePath(); */
ewing@7501
  1862
        mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, fileObject),
slouken@6630
  1863
                "getAbsolutePath", "()Ljava/lang/String;");
ewing@7501
  1864
        pathString = (jstring)(*env)->CallObjectMethod(env, fileObject, mid);
slouken@6630
  1865
ewing@7501
  1866
        path = (*env)->GetStringUTFChars(env, pathString, NULL);
slouken@6630
  1867
        s_AndroidExternalFilesPath = SDL_strdup(path);
ewing@7501
  1868
        (*env)->ReleaseStringUTFChars(env, pathString, path);
ewing@7501
  1869
ewing@7501
  1870
        LocalReferenceHolder_Cleanup(&refs);
slouken@6630
  1871
    }
slouken@6630
  1872
    return s_AndroidExternalFilesPath;
slouken@6630
  1873
}
slouken@6630
  1874
slouken@6044
  1875
#endif /* __ANDROID__ */
slouken@6044
  1876
slouken@4981
  1877
/* vi: set ts=4 sw=4 expandtab: */