src/core/android/SDL_android.cpp
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 07 Jul 2013 16:11:29 +0200
changeset 7368 0e12e09df086
parent 7246 be3beed886ea
permissions -rw-r--r--
Fixed SDL_RWread() returning -1 as unsigned instead of 0 if error on Android. Found by Cppcheck (pointed out check of unsigned < 0 which was left in place).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6864
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
5222
c66b2a778b7e Try to create an OpenGL ES 2.0 context on Android and successfully fall back to OpenGL ES 1.1 if that fails.
Sam Lantinga <slouken@libsdl.org>
parents: 5092
diff changeset
    22
#include "SDL_stdinc.h"
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
    23
#include "SDL_assert.h"
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
    24
#include "SDL_log.h"
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5996
diff changeset
    26
#ifdef __ANDROID__
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5996
diff changeset
    27
6630
55910871076b Added some platform specific API functions for Android:
Sam Lantinga <slouken@libsdl.org>
parents: 6555
diff changeset
    28
#include "SDL_system.h"
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
    29
#include "SDL_android.h"
6792
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
    30
#include <EGL/egl.h>
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
    31
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
    32
extern "C" {
5092
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    33
#include "../../events/SDL_events_c.h"
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    34
#include "../../video/android/SDL_androidkeyboard.h"
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    35
#include "../../video/android/SDL_androidtouch.h"
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    36
#include "../../video/android/SDL_androidvideo.h"
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    37
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    38
#include <android/log.h>
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    39
#include <pthread.h>
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
    40
#include <sys/types.h>
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
    41
#include <unistd.h>
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    42
#define LOG_TAG "SDL_android"
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    43
//#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    44
//#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    45
#define LOGI(...) do {} while (false)
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    46
#define LOGE(...) do {} while (false)
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    47
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
    48
/* Uncomment this to log messages entering and exiting methods in this file */
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
    49
//#define DEBUG_JNI
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    50
6191
2c0d35b1af4e Made the application activity events consistent between iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 6189
diff changeset
    51
/* Implemented in audio/android/SDL_androidaudio.c */
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    52
extern void Android_RunAudioThread();
7246
be3beed886ea Moved prototype for static function out of header file to prevent warnings.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
    53
be3beed886ea Moved prototype for static function out of header file to prevent warnings.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
    54
static void Android_JNI_ThreadDestroyed(void*);
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    55
} // C
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
    56
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
 This file links the Java side of Android with libsdl
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
*******************************************************************************/
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
#include <jni.h>
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
#include <android/log.h>
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
                               Globals
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
*******************************************************************************/
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    67
static pthread_key_t mThreadKey;
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    68
static JavaVM* mJavaVM;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    70
// Main activity
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
    71
static jclass mActivityClass;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    73
// method signatures
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    74
static jmethodID midCreateGLContext;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    75
static jmethodID midFlipBuffers;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    76
static jmethodID midAudioInit;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    77
static jmethodID midAudioWriteShortBuffer;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    78
static jmethodID midAudioWriteByteBuffer;
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
    79
static jmethodID midAudioQuit;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    81
// Accelerometer data storage
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
    82
static float fLastAccelerometer[3];
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
    83
static bool bHasNewData;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
                 Functions called by JNI
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
*******************************************************************************/
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
// Library init
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
{
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    92
    JNIEnv *env;
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    93
    mJavaVM = vm;
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    94
    LOGI("JNI_OnLoad called");
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    95
    if (mJavaVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    96
        LOGE("Failed to get the environment using GetEnv()");
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    97
        return -1;
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    98
    }
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    99
    /*
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   100
     * Create mThreadKey so we can keep track of the JNIEnv assigned to each thread
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   101
     * Refer to http://developer.android.com/guide/practices/design/jni.html for the rationale behind this
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   102
     */
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   103
    if (pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed)) {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   104
        __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing pthread key");
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   105
    }
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   106
    else {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   107
        Android_JNI_SetupThread();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   108
    }
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   109
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
    return JNI_VERSION_1_4;
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
// Called before SDL_main() to initialize JNI bindings
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   114
extern "C" void SDL_Android_Init(JNIEnv* mEnv, jclass cls)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
{
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   118
    Android_JNI_SetupThread();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   119
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   120
    mActivityClass = (jclass)mEnv->NewGlobalRef(cls);
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   121
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   122
    midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
6792
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   123
                                "createGLContext","(II[I)Z");
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   124
    midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   125
                                "flipBuffers","()V");
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   126
    midAudioInit = mEnv->GetStaticMethodID(mActivityClass,
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   127
                                "audioInit", "(IZZI)V");
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   128
    midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   129
                                "audioWriteShortBuffer", "([S)V");
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   130
    midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   131
                                "audioWriteByteBuffer", "([B)V");
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   132
    midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   133
                                "audioQuit", "()V");
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   134
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   135
    bHasNewData = false;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   136
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   137
    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   138
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   139
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
    }
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   141
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   144
// Resize
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   145
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   146
                                    JNIEnv* env, jclass jcls,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   147
                                    jint width, jint height, jint format)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
{
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   149
    Android_SetScreenResolution(width, height, format);
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   150
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   151
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   152
// Keydown
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   153
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   154
                                    JNIEnv* env, jclass jcls, jint keycode)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   155
{
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
   156
    Android_OnKeyDown(keycode);
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
// Keyup
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   160
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   161
                                    JNIEnv* env, jclass jcls, jint keycode)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
{
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
   163
    Android_OnKeyUp(keycode);
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
// Touch
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   167
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   168
                                    JNIEnv* env, jclass jcls,
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5860
diff changeset
   169
                                    jint touch_device_id_in, jint pointer_finger_id_in,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   170
                                    jint action, jfloat x, jfloat y, jfloat p)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
{
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5860
diff changeset
   172
    Android_OnTouch(touch_device_id_in, pointer_finger_id_in, action, x, y, p);
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   175
// Accelerometer
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   177
                                    JNIEnv* env, jclass jcls,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   178
                                    jfloat x, jfloat y, jfloat z)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
{
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
    fLastAccelerometer[0] = x;
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
    fLastAccelerometer[1] = y;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   182
    fLastAccelerometer[2] = z;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   183
    bHasNewData = true;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   186
// Low memory
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   187
extern "C" void Java_org_libsdl_app_SDLActivity_nativeLowMemory(
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   188
                                    JNIEnv* env, jclass cls)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   189
{
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   190
    SDL_SendAppEvent(SDL_APP_LOWMEMORY);
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   191
}
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   192
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   193
// Quit
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   194
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   195
                                    JNIEnv* env, jclass cls)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   196
{
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   197
    // Inject a SDL_QUIT event
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   198
    SDL_SendQuit();
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   199
    SDL_SendAppEvent(SDL_APP_TERMINATING);
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   200
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   201
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   202
// Pause
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   203
extern "C" void Java_org_libsdl_app_SDLActivity_nativePause(
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   204
                                    JNIEnv* env, jclass cls)
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   205
{
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   206
    if (Android_Window) {
6330
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   207
        /* Signal the pause semaphore so the event loop knows to pause and (optionally) block itself */
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   208
        if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   209
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
6191
2c0d35b1af4e Made the application activity events consistent between iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 6189
diff changeset
   210
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   211
    }
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   212
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   213
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   214
    SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   215
    SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   216
}
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   217
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   218
// Resume
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   219
extern "C" void Java_org_libsdl_app_SDLActivity_nativeResume(
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   220
                                    JNIEnv* env, jclass cls)
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   221
{
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   222
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   223
    SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   224
    SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   225
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   226
    if (Android_Window) {
6330
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   227
        /* Signal the resume semaphore so the event loop knows to resume and restore the GL Context
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   228
         * We can't restore the GL Context here because it needs to be done on the SDL main thread
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   229
         * and this function will be called from the Java thread instead.
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   230
         */
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   231
        if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   232
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
6191
2c0d35b1af4e Made the application activity events consistent between iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 6189
diff changeset
   233
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   234
    }
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   235
}
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   236
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   237
extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   238
                                    JNIEnv* env, jclass cls)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   239
{
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   240
    /* This is the audio thread, with a different environment */
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   241
    Android_JNI_SetupThread();
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   242
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   243
    Android_RunAudioThread();
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   244
}
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   245
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   246
extern "C" void Java_org_libsdl_app_SDLInputConnection_nativeCommitText(
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   247
                                    JNIEnv* env, jclass cls,
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   248
                                    jstring text, jint newCursorPosition)
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   249
{
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   250
    const char *utftext = env->GetStringUTFChars(text, NULL);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   251
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   252
    SDL_SendKeyboardText(utftext);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   253
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   254
    env->ReleaseStringUTFChars(text, utftext);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   255
}
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   256
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   257
extern "C" void Java_org_libsdl_app_SDLInputConnection_nativeSetComposingText(
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   258
                                    JNIEnv* env, jclass cls,
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   259
                                    jstring text, jint newCursorPosition)
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   260
{
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   261
    const char *utftext = env->GetStringUTFChars(text, NULL);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   262
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   263
    SDL_SendEditingText(utftext, 0, 0);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   264
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   265
    env->ReleaseStringUTFChars(text, utftext);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   266
}
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   267
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   268
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   269
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   271
             Functions called by SDL into Java
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   272
*******************************************************************************/
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   273
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   274
class LocalReferenceHolder
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   275
{
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   276
private:
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   277
    static int s_active;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   278
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   279
public:
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   280
    static bool IsActive() {
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   281
        return s_active > 0;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   282
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   283
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   284
public:
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   285
    LocalReferenceHolder(const char *func) : m_env(NULL), m_func(func) {
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   286
#ifdef DEBUG_JNI
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   287
        SDL_Log("Entering function %s", m_func);
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   288
#endif
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   289
    }
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   290
    ~LocalReferenceHolder() {
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   291
#ifdef DEBUG_JNI
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   292
        SDL_Log("Leaving function %s", m_func);
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   293
#endif
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   294
        if (m_env) {
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   295
            m_env->PopLocalFrame(NULL);
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   296
            --s_active;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   297
        }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   298
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   299
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   300
    bool init(JNIEnv *env, jint capacity = 16) {
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   301
        if (env->PushLocalFrame(capacity) < 0) {
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   302
            SDL_SetError("Failed to allocate enough JVM local references");
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   303
            return false;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   304
        }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   305
        ++s_active;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   306
        m_env = env;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   307
        return true;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   308
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   309
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   310
protected:
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   311
    JNIEnv *m_env;
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   312
    const char *m_func;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   313
};
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   314
int LocalReferenceHolder::s_active;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   315
6792
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   316
extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   317
                                int red, int green, int blue, int alpha,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   318
                                int buffer, int depth, int stencil,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   319
                                int buffers, int samples)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   320
{
6792
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   321
    JNIEnv *env = Android_JNI_GetEnv();
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   322
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   323
    jint attribs[] = {
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   324
        EGL_RED_SIZE, red,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   325
        EGL_GREEN_SIZE, green,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   326
        EGL_BLUE_SIZE, blue,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   327
        EGL_ALPHA_SIZE, alpha,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   328
        EGL_BUFFER_SIZE, buffer,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   329
        EGL_DEPTH_SIZE, depth,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   330
        EGL_STENCIL_SIZE, stencil,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   331
        EGL_SAMPLE_BUFFERS, buffers,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   332
        EGL_SAMPLES, samples,
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   333
        EGL_RENDERABLE_TYPE, (majorVersion == 1 ? EGL_OPENGL_ES_BIT : EGL_OPENGL_ES2_BIT),
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   334
        EGL_NONE
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   335
    };
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   336
    int len = SDL_arraysize(attribs);
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   337
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   338
    jintArray array;
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   339
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   340
    array = env->NewIntArray(len);
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   341
    env->SetIntArrayRegion(array, 0, len, attribs);
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   342
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   343
    jboolean success = env->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion, array);
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   344
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   345
    env->DeleteLocalRef(array);
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   346
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   347
    return success ? SDL_TRUE : SDL_FALSE;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   348
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   349
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
   350
extern "C" void Android_JNI_SwapWindow()
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   351
{
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   352
    JNIEnv *mEnv = Android_JNI_GetEnv();
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   353
    mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers);
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   354
}
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   355
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   356
extern "C" void Android_JNI_SetActivityTitle(const char *title)
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   357
{
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   358
    jmethodID mid;
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   359
    JNIEnv *mEnv = Android_JNI_GetEnv();
7149
534891ad4897 Changed signatures of methods in Java file to return boolean, adapted C++ file.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7130
diff changeset
   360
    mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)Z");
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   361
    if (mid) {
6307
6048116f40b1 Fix for issue #1465, Leak in Android_JNI_SetActivityTitle (SDL_SetWindowTitle)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6284
diff changeset
   362
        jstring jtitle = reinterpret_cast<jstring>(mEnv->NewStringUTF(title));
7149
534891ad4897 Changed signatures of methods in Java file to return boolean, adapted C++ file.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7130
diff changeset
   363
        mEnv->CallStaticBooleanMethod(mActivityClass, mid, jtitle);
6307
6048116f40b1 Fix for issue #1465, Leak in Android_JNI_SetActivityTitle (SDL_SetWindowTitle)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6284
diff changeset
   364
        mEnv->DeleteLocalRef(jtitle);
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   365
    }
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   366
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   367
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   368
extern "C" SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   369
{
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   370
    int i;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   371
    SDL_bool retval = SDL_FALSE;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   372
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   373
    if (bHasNewData) {
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   374
        for (i = 0; i < 3; ++i) {
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   375
            values[i] = fLastAccelerometer[i];
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   376
        }
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   377
        bHasNewData = false;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   378
        retval = SDL_TRUE;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   379
    }
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   380
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   381
    return retval;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   382
}
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   383
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   384
static void Android_JNI_ThreadDestroyed(void* value) {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   385
    /* The thread is being destroyed, detach it from the Java VM and set the mThreadKey value to NULL as required */
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   386
    JNIEnv *env = (JNIEnv*) value;
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   387
    if (env != NULL) {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   388
        mJavaVM->DetachCurrentThread();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   389
        pthread_setspecific(mThreadKey, NULL);
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   390
    }
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   391
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   392
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   393
JNIEnv* Android_JNI_GetEnv(void) {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   394
    /* From http://developer.android.com/guide/practices/jni.html
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   395
     * All threads are Linux threads, scheduled by the kernel.
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   396
     * They're usually started from managed code (using Thread.start), but they can also be created elsewhere and then
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   397
     * attached to the JavaVM. For example, a thread started with pthread_create can be attached with the
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   398
     * JNI AttachCurrentThread or AttachCurrentThreadAsDaemon functions. Until a thread is attached, it has no JNIEnv,
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   399
     * and cannot make JNI calls.
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   400
     * Attaching a natively-created thread causes a java.lang.Thread object to be constructed and added to the "main"
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   401
     * ThreadGroup, making it visible to the debugger. Calling AttachCurrentThread on an already-attached thread
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   402
     * is a no-op.
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   403
     * Note: You can call this function any number of times for the same thread, there's no harm in it
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   404
     */
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   405
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   406
    JNIEnv *env;
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   407
    int status = mJavaVM->AttachCurrentThread(&env, NULL);
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   408
    if(status < 0) {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   409
        LOGE("failed to attach current thread");
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   410
        return 0;
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   411
    }
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   412
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   413
    return env;
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   414
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   415
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   416
int Android_JNI_SetupThread(void) {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   417
    /* From http://developer.android.com/guide/practices/jni.html
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   418
     * Threads attached through JNI must call DetachCurrentThread before they exit. If coding this directly is awkward,
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   419
     * in Android 2.0 (Eclair) and higher you can use pthread_key_create to define a destructor function that will be
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   420
     * called before the thread exits, and call DetachCurrentThread from there. (Use that key with pthread_setspecific
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   421
     * to store the JNIEnv in thread-local-storage; that way it'll be passed into your destructor as the argument.)
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   422
     * Note: The destructor is not called unless the stored value is != NULL
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   423
     * Note: You can call this function any number of times for the same thread, there's no harm in it
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   424
     *       (except for some lost CPU cycles)
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   425
     */
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   426
    JNIEnv *env = Android_JNI_GetEnv();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   427
    pthread_setspecific(mThreadKey, (void*) env);
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   428
    return 1;
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   429
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   430
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   431
//
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   432
// Audio support
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   433
//
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   434
static jboolean audioBuffer16Bit = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   435
static jboolean audioBufferStereo = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   436
static jobject audioBuffer = NULL;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   437
static void* audioBufferPinned = NULL;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   438
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   439
extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   440
{
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   441
    int audioBufferFrames;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   442
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   443
    JNIEnv *env = Android_JNI_GetEnv();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   444
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   445
    if (!env) {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   446
        LOGE("callback_handler: failed to attach current thread");
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   447
    }
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   448
    Android_JNI_SetupThread();
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   449
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   450
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   451
    audioBuffer16Bit = is16Bit;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   452
    audioBufferStereo = channelCount > 1;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   453
6864
97187387ad79 Bug 1703 - SDL calls a wrong JNI function on Android by Philipp Wiesemann
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6828
diff changeset
   454
    env->CallStaticVoidMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   455
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   456
    /* Allocating the audio buffer from the Java side and passing it as the return value for audioInit no longer works on
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   457
     * Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   458
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   459
    if (is16Bit) {
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   460
        jshortArray audioBufferLocal = env->NewShortArray(desiredBufferFrames * (audioBufferStereo ? 2 : 1));
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   461
        if (audioBufferLocal) {
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   462
            audioBuffer = env->NewGlobalRef(audioBufferLocal);
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   463
            env->DeleteLocalRef(audioBufferLocal);
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   464
        }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   465
    }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   466
    else {
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   467
        jbyteArray audioBufferLocal = env->NewByteArray(desiredBufferFrames * (audioBufferStereo ? 2 : 1));
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   468
        if (audioBufferLocal) {
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   469
            audioBuffer = env->NewGlobalRef(audioBufferLocal);
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   470
            env->DeleteLocalRef(audioBufferLocal);
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   471
        }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   472
    }
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   473
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   474
    if (audioBuffer == NULL) {
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   475
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: could not allocate an audio buffer!");
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   476
        return 0;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   477
    }
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   478
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   479
    jboolean isCopy = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   480
    if (audioBuffer16Bit) {
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   481
        audioBufferPinned = env->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   482
        audioBufferFrames = env->GetArrayLength((jshortArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   483
    } else {
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   484
        audioBufferPinned = env->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   485
        audioBufferFrames = env->GetArrayLength((jbyteArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   486
    }
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   487
    if (audioBufferStereo) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   488
        audioBufferFrames /= 2;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   489
    }
6864
97187387ad79 Bug 1703 - SDL calls a wrong JNI function on Android by Philipp Wiesemann
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6828
diff changeset
   490
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   491
    return audioBufferFrames;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   492
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   493
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   494
extern "C" void * Android_JNI_GetAudioBuffer()
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   495
{
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   496
    return audioBufferPinned;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   497
}
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   498
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   499
extern "C" void Android_JNI_WriteAudioBuffer()
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   500
{
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   501
    JNIEnv *mAudioEnv = Android_JNI_GetEnv();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   502
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   503
    if (audioBuffer16Bit) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   504
        mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   505
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   506
    } else {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   507
        mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   508
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   509
    }
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   510
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   511
    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   512
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   513
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   514
extern "C" void Android_JNI_CloseAudioDevice()
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   515
{
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   516
    JNIEnv *env = Android_JNI_GetEnv();
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   517
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   518
    env->CallStaticVoidMethod(mActivityClass, midAudioQuit);
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   519
4997
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   520
    if (audioBuffer) {
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   521
        env->DeleteGlobalRef(audioBuffer);
4997
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   522
        audioBuffer = NULL;
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   523
        audioBufferPinned = NULL;
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   524
    }
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   525
}
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
   526
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   527
// Test for an exception and call SDL_SetError with its detail if one occurs
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   528
// If optional parameter silent is truthy then SDL_SetError() is not called.
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   529
static bool Android_JNI_ExceptionOccurred(bool silent = false)
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   530
{
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   531
    SDL_assert(LocalReferenceHolder::IsActive());
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   532
    JNIEnv *mEnv = Android_JNI_GetEnv();
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   533
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   534
    jthrowable exception = mEnv->ExceptionOccurred();
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   535
    if (exception != NULL) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   536
        jmethodID mid;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   537
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   538
        // Until this happens most JNI operations have undefined behaviour
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   539
        mEnv->ExceptionClear();
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   540
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   541
        if (!silent) {
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   542
            jclass exceptionClass = mEnv->GetObjectClass(exception);
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   543
            jclass classClass = mEnv->FindClass("java/lang/Class");
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   544
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   545
            mid = mEnv->GetMethodID(classClass, "getName", "()Ljava/lang/String;");
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   546
            jstring exceptionName = (jstring)mEnv->CallObjectMethod(exceptionClass, mid);
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   547
            const char* exceptionNameUTF8 = mEnv->GetStringUTFChars(exceptionName, 0);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   548
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   549
            mid = mEnv->GetMethodID(exceptionClass, "getMessage", "()Ljava/lang/String;");
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   550
            jstring exceptionMessage = (jstring)mEnv->CallObjectMethod(exception, mid);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   551
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   552
            if (exceptionMessage != NULL) {
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   553
                const char* exceptionMessageUTF8 = mEnv->GetStringUTFChars(exceptionMessage, 0);
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   554
                SDL_SetError("%s: %s", exceptionNameUTF8, exceptionMessageUTF8);
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   555
                mEnv->ReleaseStringUTFChars(exceptionMessage, exceptionMessageUTF8);
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   556
            } else {
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   557
                SDL_SetError("%s", exceptionNameUTF8);
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   558
            }
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   559
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   560
            mEnv->ReleaseStringUTFChars(exceptionName, exceptionNameUTF8);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   561
        }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   562
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   563
        return true;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   564
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   565
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   566
    return false;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   567
}
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   568
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   569
static int Android_JNI_FileOpen(SDL_RWops* ctx)
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   570
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   571
    LocalReferenceHolder refs(__FUNCTION__);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   572
    int result = 0;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   573
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   574
    jmethodID mid;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   575
    jobject context;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   576
    jobject assetManager;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   577
    jobject inputStream;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   578
    jclass channels;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   579
    jobject readableByteChannel;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   580
    jstring fileNameJString;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   581
    jobject fd;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   582
    jclass fdCls;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   583
    jfieldID descriptor;
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   584
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   585
    JNIEnv *mEnv = Android_JNI_GetEnv();
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   586
    if (!refs.init(mEnv)) {
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   587
        goto failure;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   588
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   589
6308
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   590
    fileNameJString = (jstring)ctx->hidden.androidio.fileNameRef;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   591
    ctx->hidden.androidio.position = 0;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   592
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   593
    // context = SDLActivity.getContext();
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   594
    mid = mEnv->GetStaticMethodID(mActivityClass,
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   595
            "getContext","()Landroid/content/Context;");
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   596
    context = mEnv->CallStaticObjectMethod(mActivityClass, mid);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   597
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   598
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   599
    // assetManager = context.getAssets();
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   600
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(context),
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   601
            "getAssets", "()Landroid/content/res/AssetManager;");
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   602
    assetManager = mEnv->CallObjectMethod(context, mid);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   603
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   604
    /* First let's try opening the file to obtain an AssetFileDescriptor.
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   605
    * This method reads the files directly from the APKs using standard *nix calls
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   606
    */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   607
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(assetManager), "openFd", "(Ljava/lang/String;)Landroid/content/res/AssetFileDescriptor;");
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   608
    inputStream = mEnv->CallObjectMethod(assetManager, mid, fileNameJString);
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   609
    if (Android_JNI_ExceptionOccurred(true)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   610
        goto fallback;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   611
    }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   612
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   613
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream), "getStartOffset", "()J");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   614
    ctx->hidden.androidio.offset = mEnv->CallLongMethod(inputStream, mid);
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   615
    if (Android_JNI_ExceptionOccurred(true)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   616
        goto fallback;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   617
    }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   618
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   619
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream), "getDeclaredLength", "()J");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   620
    ctx->hidden.androidio.size = mEnv->CallLongMethod(inputStream, mid);
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   621
    if (Android_JNI_ExceptionOccurred(true)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   622
        goto fallback;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   623
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   624
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   625
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream), "getFileDescriptor", "()Ljava/io/FileDescriptor;");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   626
    fd = mEnv->CallObjectMethod(inputStream, mid);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   627
    fdCls = mEnv->GetObjectClass(fd);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   628
    descriptor = mEnv->GetFieldID(fdCls, "descriptor", "I");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   629
    ctx->hidden.androidio.fd = mEnv->GetIntField(fd, descriptor);
6828
ac7f004fb63c Fixes potential global reference leak on Android, by Philipp Wiesemann
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6816
diff changeset
   630
    ctx->hidden.androidio.assetFileDescriptorRef = mEnv->NewGlobalRef(inputStream);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   631
6816
b3d3ef1e15b5 Fixes bug 1696, thanks Paul Geerts!
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6806
diff changeset
   632
    // Seek to the correct offset in the file.
b3d3ef1e15b5 Fixes bug 1696, thanks Paul Geerts!
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6806
diff changeset
   633
    lseek(ctx->hidden.androidio.fd, (off_t)ctx->hidden.androidio.offset, SEEK_SET);
b3d3ef1e15b5 Fixes bug 1696, thanks Paul Geerts!
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6806
diff changeset
   634
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   635
    if (false) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   636
fallback:
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   637
        // Disabled log message because of spam on the Nexus 7
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   638
        //__android_log_print(ANDROID_LOG_DEBUG, "SDL", "Falling back to legacy InputStream method for opening file");
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   639
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   640
        /* Try the old method using InputStream */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   641
        ctx->hidden.androidio.assetFileDescriptorRef = NULL;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   642
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   643
        // inputStream = assetManager.open(<filename>);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   644
        mid = mEnv->GetMethodID(mEnv->GetObjectClass(assetManager),
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   645
                "open", "(Ljava/lang/String;I)Ljava/io/InputStream;");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   646
        inputStream = mEnv->CallObjectMethod(assetManager, mid, fileNameJString, 1 /*ACCESS_RANDOM*/);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   647
        if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   648
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   649
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   650
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   651
        ctx->hidden.androidio.inputStreamRef = mEnv->NewGlobalRef(inputStream);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   652
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   653
        // Despite all the visible documentation on [Asset]InputStream claiming
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   654
        // that the .available() method is not guaranteed to return the entire file
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   655
        // size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   656
        // android/apis/content/ReadAsset.java imply that Android's
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   657
        // AssetInputStream.available() /will/ always return the total file size
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   658
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   659
        // size = inputStream.available();
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   660
        mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   661
                "available", "()I");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   662
        ctx->hidden.androidio.size = (long)mEnv->CallIntMethod(inputStream, mid);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   663
        if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   664
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   665
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   666
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   667
        // readableByteChannel = Channels.newChannel(inputStream);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   668
        channels = mEnv->FindClass("java/nio/channels/Channels");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   669
        mid = mEnv->GetStaticMethodID(channels,
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   670
                "newChannel",
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   671
                "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   672
        readableByteChannel = mEnv->CallStaticObjectMethod(
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   673
                channels, mid, inputStream);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   674
        if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   675
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   676
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   677
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   678
        ctx->hidden.androidio.readableByteChannelRef =
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   679
            mEnv->NewGlobalRef(readableByteChannel);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   680
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   681
        // Store .read id for reading purposes
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   682
        mid = mEnv->GetMethodID(mEnv->GetObjectClass(readableByteChannel),
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   683
                "read", "(Ljava/nio/ByteBuffer;)I");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   684
        ctx->hidden.androidio.readMethod = mid;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   685
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   686
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   687
    if (false) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   688
failure:
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   689
        result = -1;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   690
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   691
        mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   692
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   693
        if(ctx->hidden.androidio.inputStreamRef != NULL) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   694
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   695
        }
6308
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   696
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   697
        if(ctx->hidden.androidio.readableByteChannelRef != NULL) {
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   698
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.readableByteChannelRef);
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   699
        }
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   700
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   701
        if(ctx->hidden.androidio.assetFileDescriptorRef != NULL) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   702
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.assetFileDescriptorRef);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   703
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   704
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   705
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   706
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   707
    return result;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   708
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   709
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   710
extern "C" int Android_JNI_FileOpen(SDL_RWops* ctx,
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   711
        const char* fileName, const char*)
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   712
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   713
    LocalReferenceHolder refs(__FUNCTION__);
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   714
    JNIEnv *mEnv = Android_JNI_GetEnv();
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   715
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   716
    if (!refs.init(mEnv)) {
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   717
        return -1;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   718
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   719
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   720
    if (!ctx) {
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   721
        return -1;
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   722
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   723
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   724
    jstring fileNameJString = mEnv->NewStringUTF(fileName);
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   725
    ctx->hidden.androidio.fileNameRef = mEnv->NewGlobalRef(fileNameJString);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   726
    ctx->hidden.androidio.inputStreamRef = NULL;
6335
fbb84f5b985f Fixes #1519, adds initialization to Android RWops internal variables.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6330
diff changeset
   727
    ctx->hidden.androidio.readableByteChannelRef = NULL;
fbb84f5b985f Fixes #1519, adds initialization to Android RWops internal variables.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6330
diff changeset
   728
    ctx->hidden.androidio.readMethod = NULL;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   729
    ctx->hidden.androidio.assetFileDescriptorRef = NULL;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   730
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   731
    return Android_JNI_FileOpen(ctx);
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   732
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   733
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   734
extern "C" size_t Android_JNI_FileRead(SDL_RWops* ctx, void* buffer,
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   735
        size_t size, size_t maxnum)
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   736
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   737
    LocalReferenceHolder refs(__FUNCTION__);
6377
3d868ca4782f Fixes Android_JNI_FileRead behaviour where reading past the end of a file returns zero instead of the number of bytes read.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6354
diff changeset
   738
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   739
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   740
        size_t bytesMax = size * maxnum;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   741
        if (ctx->hidden.androidio.size != -1 /*UNKNOWN_LENGTH*/ && ctx->hidden.androidio.position + bytesMax > ctx->hidden.androidio.size) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   742
            bytesMax = ctx->hidden.androidio.size - ctx->hidden.androidio.position;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   743
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   744
        size_t result = read(ctx->hidden.androidio.fd, buffer, bytesMax );
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   745
        if (result > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   746
            ctx->hidden.androidio.position += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   747
            return result / size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   748
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   749
        return 0;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   750
    } else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   751
        jlong bytesRemaining = (jlong) (size * maxnum);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   752
        jlong bytesMax = (jlong) (ctx->hidden.androidio.size -  ctx->hidden.androidio.position);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   753
        int bytesRead = 0;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   754
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   755
        /* Don't read more bytes than those that remain in the file, otherwise we get an exception */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   756
        if (bytesRemaining >  bytesMax) bytesRemaining = bytesMax;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   757
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   758
        JNIEnv *mEnv = Android_JNI_GetEnv();
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   759
        if (!refs.init(mEnv)) {
7368
0e12e09df086 Fixed SDL_RWread() returning -1 as unsigned instead of 0 if error on Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7246
diff changeset
   760
            return 0;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   761
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   762
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   763
        jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannelRef;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   764
        jmethodID readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   765
        jobject byteBuffer = mEnv->NewDirectByteBuffer(buffer, bytesRemaining);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   766
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   767
        while (bytesRemaining > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   768
            // result = readableByteChannel.read(...);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   769
            int result = mEnv->CallIntMethod(readableByteChannel, readMethod, byteBuffer);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   770
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   771
            if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   772
                return 0;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   773
            }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   774
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   775
            if (result < 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   776
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   777
            }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   778
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   779
            bytesRemaining -= result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   780
            bytesRead += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   781
            ctx->hidden.androidio.position += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   782
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   783
        return bytesRead / size;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   784
    }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   785
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   786
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   787
extern "C" size_t Android_JNI_FileWrite(SDL_RWops* ctx, const void* buffer,
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   788
        size_t size, size_t num)
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   789
{
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   790
    SDL_SetError("Cannot write to Android package filesystem");
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   791
    return 0;
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   792
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   793
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   794
static int Android_JNI_FileClose(SDL_RWops* ctx, bool release)
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   795
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   796
    LocalReferenceHolder refs(__FUNCTION__);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   797
    int result = 0;
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   798
    JNIEnv *mEnv = Android_JNI_GetEnv();
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   799
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   800
    if (!refs.init(mEnv)) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   801
        return SDL_SetError("Failed to allocate enough JVM local references");
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   802
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   803
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   804
    if (ctx) {
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   805
        if (release) {
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   806
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.fileNameRef);
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   807
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   808
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   809
        if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   810
            jobject inputStream = (jobject)ctx->hidden.androidio.assetFileDescriptorRef;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   811
            jmethodID mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   812
                    "close", "()V");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   813
            mEnv->CallVoidMethod(inputStream, mid);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   814
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.assetFileDescriptorRef);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   815
            if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   816
                result = -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   817
            }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   818
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   819
        else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   820
            jobject inputStream = (jobject)ctx->hidden.androidio.inputStreamRef;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   821
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   822
            // inputStream.close();
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   823
            jmethodID mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   824
                    "close", "()V");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   825
            mEnv->CallVoidMethod(inputStream, mid);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   826
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   827
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.readableByteChannelRef);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   828
            if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   829
                result = -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   830
            }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   831
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   832
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   833
        if (release) {
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   834
            SDL_FreeRW(ctx);
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   835
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   836
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   837
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   838
    return result;
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   839
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   840
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   841
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   842
extern "C" Sint64 Android_JNI_FileSize(SDL_RWops* ctx)
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   843
{
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   844
    return ctx->hidden.androidio.size;
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   845
}
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   846
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   847
extern "C" Sint64 Android_JNI_FileSeek(SDL_RWops* ctx, Sint64 offset, int whence)
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   848
{
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   849
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   850
        switch (whence) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   851
            case RW_SEEK_SET:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   852
                if (ctx->hidden.androidio.size != -1 /*UNKNOWN_LENGTH*/ && offset > ctx->hidden.androidio.size) offset = ctx->hidden.androidio.size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   853
                offset += ctx->hidden.androidio.offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   854
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   855
            case RW_SEEK_CUR:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   856
                offset += ctx->hidden.androidio.position;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   857
                if (ctx->hidden.androidio.size != -1 /*UNKNOWN_LENGTH*/ && offset > ctx->hidden.androidio.size) offset = ctx->hidden.androidio.size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   858
                offset += ctx->hidden.androidio.offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   859
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   860
            case RW_SEEK_END:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   861
                offset = ctx->hidden.androidio.offset + ctx->hidden.androidio.size + offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   862
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   863
            default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   864
                return SDL_SetError("Unknown value for 'whence'");
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   865
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   866
        whence = SEEK_SET;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   867
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   868
        off_t ret = lseek(ctx->hidden.androidio.fd, (off_t)offset, SEEK_SET);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   869
        if (ret == -1) return -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   870
        ctx->hidden.androidio.position = ret - ctx->hidden.androidio.offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   871
    } else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   872
        Sint64 newPosition;
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   873
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   874
        switch (whence) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   875
            case RW_SEEK_SET:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   876
                newPosition = offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   877
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   878
            case RW_SEEK_CUR:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   879
                newPosition = ctx->hidden.androidio.position + offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   880
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   881
            case RW_SEEK_END:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   882
                newPosition = ctx->hidden.androidio.size + offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   883
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   884
            default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   885
                return SDL_SetError("Unknown value for 'whence'");
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   886
        }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   887
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   888
        /* Validate the new position */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   889
        if (newPosition < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   890
            return SDL_Error(SDL_EFSEEK);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   891
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   892
        if (newPosition > ctx->hidden.androidio.size) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   893
            newPosition = ctx->hidden.androidio.size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   894
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   895
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   896
        Sint64 movement = newPosition - ctx->hidden.androidio.position;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   897
        if (movement > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   898
            unsigned char buffer[4096];
5993
c9cb52d6d864 * Android's InputStream::skip is apparently buggy, so instead read into a dummy buffer
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5982
diff changeset
   899
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   900
            // The easy case where we're seeking forwards
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   901
            while (movement > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   902
                Sint64 amount = sizeof (buffer);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   903
                if (amount > movement) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   904
                    amount = movement;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   905
                }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   906
                size_t result = Android_JNI_FileRead(ctx, buffer, 1, amount);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   907
                if (result <= 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   908
                    // Failed to read/skip the required amount, so fail
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   909
                    return -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   910
                }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   911
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   912
                movement -= result;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   913
            }
5993
c9cb52d6d864 * Android's InputStream::skip is apparently buggy, so instead read into a dummy buffer
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5982
diff changeset
   914
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   915
        } else if (movement < 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   916
            // We can't seek backwards so we have to reopen the file and seek
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   917
            // forwards which obviously isn't very efficient
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   918
            Android_JNI_FileClose(ctx, false);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   919
            Android_JNI_FileOpen(ctx);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   920
            Android_JNI_FileSeek(ctx, newPosition, RW_SEEK_SET);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   921
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   922
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   923
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   924
    return ctx->hidden.androidio.position;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   925
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   926
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   927
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   928
extern "C" int Android_JNI_FileClose(SDL_RWops* ctx)
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   929
{
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   930
    return Android_JNI_FileClose(ctx, true);
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   931
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   932
6464
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   933
// returns a new global reference which needs to be released later
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   934
static jobject Android_JNI_GetSystemServiceObject(const char* name)
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   935
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   936
    LocalReferenceHolder refs(__FUNCTION__);
6464
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   937
    JNIEnv* env = Android_JNI_GetEnv();
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   938
    if (!refs.init(env)) {
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   939
        return NULL;
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   940
    }
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   941
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   942
    jstring service = env->NewStringUTF(name);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   943
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   944
    jmethodID mid;
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   945
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   946
    mid = env->GetStaticMethodID(mActivityClass, "getContext", "()Landroid/content/Context;");
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   947
    jobject context = env->CallStaticObjectMethod(mActivityClass, mid);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   948
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   949
    mid = env->GetMethodID(mActivityClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   950
    jobject manager = env->CallObjectMethod(context, mid, service);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   951
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   952
    env->DeleteLocalRef(service);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   953
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   954
    return manager ? env->NewGlobalRef(manager) : NULL;
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   955
}
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   956
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   957
#define SETUP_CLIPBOARD(error) \
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   958
    LocalReferenceHolder refs(__FUNCTION__); \
6464
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   959
    JNIEnv* env = Android_JNI_GetEnv(); \
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   960
    if (!refs.init(env)) { \
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   961
        return error; \
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   962
    } \
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   963
    jobject clipboard = Android_JNI_GetSystemServiceObject("clipboard"); \
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   964
    if (!clipboard) { \
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   965
        return error; \
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   966
    }
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   967
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   968
extern "C" int Android_JNI_SetClipboardText(const char* text)
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   969
{
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   970
    SETUP_CLIPBOARD(-1)
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   971
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   972
    jmethodID mid = env->GetMethodID(env->GetObjectClass(clipboard), "setText", "(Ljava/lang/CharSequence;)V");
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   973
    jstring string = env->NewStringUTF(text);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   974
    env->CallVoidMethod(clipboard, mid, string);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   975
    env->DeleteGlobalRef(clipboard);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   976
    env->DeleteLocalRef(string);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   977
    return 0;
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   978
}
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   979
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   980
extern "C" char* Android_JNI_GetClipboardText()
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   981
{
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   982
    SETUP_CLIPBOARD(SDL_strdup(""))
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   983
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   984
    jmethodID mid = env->GetMethodID(env->GetObjectClass(clipboard), "getText", "()Ljava/lang/CharSequence;");
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   985
    jobject sequence = env->CallObjectMethod(clipboard, mid);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   986
    env->DeleteGlobalRef(clipboard);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   987
    if (sequence) {
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   988
        mid = env->GetMethodID(env->GetObjectClass(sequence), "toString", "()Ljava/lang/String;");
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   989
        jstring string = reinterpret_cast<jstring>(env->CallObjectMethod(sequence, mid));
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   990
        const char* utf = env->GetStringUTFChars(string, 0);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   991
        if (utf) {
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   992
            char* text = SDL_strdup(utf);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   993
            env->ReleaseStringUTFChars(string, utf);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   994
            return text;
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   995
        }
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   996
    }
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   997
    return SDL_strdup("");
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   998
}
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   999
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
  1000
extern "C" SDL_bool Android_JNI_HasClipboardText()
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
  1001
{
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
  1002
    SETUP_CLIPBOARD(SDL_FALSE)
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
  1003
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
  1004
    jmethodID mid = env->GetMethodID(env->GetObjectClass(clipboard), "hasText", "()Z");
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
  1005
    jboolean has = env->CallBooleanMethod(clipboard, mid);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
  1006
    env->DeleteGlobalRef(clipboard);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: