src/core/android/SDL_android.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 25 Sep 2018 20:08:51 -0700
changeset 12224 0582b4e1fa9f
parent 12201 8bdc4d340419
child 12242 df7260f149f2
permissions -rw-r--r--
Fixed bug 4268 - Android_JNI_OpenAudioDevice function has error

alittle

where iscapture == 1

1.param set error

(*env)->CallStaticIntMethod(env, mAudioManagerClass, midCaptureOpen, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames)

param:audioBuffer16Bit need change to captureBuffer16Bit

2.logic error

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