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