src/core/android/SDL_android.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 12 Aug 2017 12:24:59 -0700
changeset 11239 856e5e3c1e86
parent 11238 c728c661cec7
child 11270 31ce9a8ce2bd
permissions -rw-r--r--
Fixed bug 3128 - Removing all the static variables from android SDLActivity and accompanying JNI calls.

owen

I removed all the static variables from SDLActivity.java

Updated all the SDL_android.c jni calls as well

I added a new function to SDL_android.c/ h
void Android_JNI_SeparateEventsHint(const char* c);

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