src/core/android/SDL_android.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 02 Nov 2018 17:18:03 -0700
changeset 12387 2f02c753a5dc
parent 12381 dc9108cd4340
child 12464 db15b3c90139
permissions -rw-r--r--
Fixed bug 4315 - little Warning in Android_JNI_CaptureAudioBuffer

Sylvain

SDL_android.c
src/core/android/SDL_android.c:1302:5: warning: variable 'br' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]
default:
^~~~~~~
src/core/android/SDL_android.c:1306:12: note: uninitialized use occurs here
return br;
^~
src/core/android/SDL_android.c:1270:12: note: initialize the variable 'br' to silence this warning
jint br;
^


Maybe we could add some basics warning flags, not to see all warnings, but so that new warnings are caught sooner.

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