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