src/core/android/SDL_android.cpp
author Gabriel Jacobo <gabomdq@gmail.com>
Mon, 14 Jan 2013 20:20:33 -0300
changeset 6816 b3d3ef1e15b5
parent 6806 9e57ff36fd7a
child 6828 ac7f004fb63c
permissions -rw-r--r--
Fixes bug 1696, thanks Paul Geerts! Also adds a bit of information about asset loading on Android.
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
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
     3
  Copyright (C) 1997-2012 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();
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    53
} // C
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
    54
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
    55
/*******************************************************************************
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
    56
 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
    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
#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
    59
#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
    60
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
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
                               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
    64
*******************************************************************************/
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
    65
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
    66
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
    67
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    68
// 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
    69
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
    70
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    71
// method signatures
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    72
static jmethodID midCreateGLContext;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    73
static jmethodID midFlipBuffers;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    74
static jmethodID midAudioInit;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    75
static jmethodID midAudioWriteShortBuffer;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    76
static jmethodID midAudioWriteByteBuffer;
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
    77
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
    78
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    79
// Accelerometer data storage
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
    80
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
    81
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
    82
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
    83
/*******************************************************************************
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
                 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
    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
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
// 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
    88
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
    89
{
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
    90
    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
    91
    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
    92
    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
    93
    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
    94
        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
    95
        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
    96
    }
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
    97
    /*
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    98
     * 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
    99
     * 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
   100
     */
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
    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
   102
        __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
   103
    }
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
    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
   105
        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
   106
    }
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
   107
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
   108
    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
   109
}
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
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
// 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
   112
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
   113
{
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
   114
    __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
   115
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
   116
    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
   117
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
    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
   119
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   120
    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
   121
                                "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
   122
    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
   123
                                "flipBuffers","()V");
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   124
    midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   125
                                "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
   126
    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
   127
                                "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
   128
    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
   129
                                "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
   130
    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
   131
                                "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
   132
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   133
    bHasNewData = false;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   134
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   135
    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   136
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   137
        __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
   138
    }
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
   139
    __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
   140
}
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
   141
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   142
// Resize
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   143
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
   144
                                    JNIEnv* env, jclass jcls,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   145
                                    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
   146
{
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   147
    Android_SetScreenResolution(width, height, format);
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   148
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   149
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   150
// Keydown
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   151
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
   152
                                    JNIEnv* env, jclass jcls, jint keycode)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   153
{
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
   154
    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
   155
}
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
   156
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
// Keyup
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   158
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
   159
                                    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
   160
{
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
   161
    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
   162
}
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
   163
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
// Touch
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   165
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
   166
                                    JNIEnv* env, jclass jcls,
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5860
diff changeset
   167
                                    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
   168
                                    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
   169
{
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5860
diff changeset
   170
    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
   171
}
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
   172
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   173
// 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
   174
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
   175
                                    JNIEnv* env, jclass jcls,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   176
                                    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
   177
{
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
   178
    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
   179
    fLastAccelerometer[1] = y;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   180
    fLastAccelerometer[2] = z;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   181
    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
   182
}
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
   183
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   184
// Quit
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   185
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
   186
                                    JNIEnv* env, jclass cls)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   187
{    
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   188
    // Inject a SDL_QUIT event
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   189
    SDL_SendQuit();
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   190
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   191
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   192
// Pause
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   193
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
   194
                                    JNIEnv* env, jclass cls)
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   195
{
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   196
    if (Android_Window) {
6330
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   197
        /* 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
   198
        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
   199
        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
   200
        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
   201
    }
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   202
}
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   203
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   204
// Resume
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   205
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
   206
                                    JNIEnv* env, jclass cls)
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   207
{
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   208
    if (Android_Window) {
6330
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   209
        /* 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
   210
         * 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
   211
         * 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
   212
         */
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   213
        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
   214
        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
   215
        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
   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
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   219
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
   220
                                    JNIEnv* env, jclass cls)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   221
{
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   222
    /* 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
   223
    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
   224
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   225
    Android_RunAudioThread();
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   226
}
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
   227
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   228
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
   229
                                    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
   230
                                    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
   231
{
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   232
    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
   233
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   234
    SDL_SendKeyboardText(utftext);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   235
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   236
    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
   237
}
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   238
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   239
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
   240
                                    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
   241
                                    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
   242
{
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   243
    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
   244
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   245
    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
   246
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   247
    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
   248
}
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
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   251
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
   252
/*******************************************************************************
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
   253
             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
   254
*******************************************************************************/
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   255
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   256
class LocalReferenceHolder
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   257
{
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   258
private:
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   259
    static int s_active;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   260
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   261
public:
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   262
    static bool IsActive() {
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   263
        return s_active > 0;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   264
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   265
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   266
public:
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   267
    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
   268
#ifdef DEBUG_JNI
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   269
        SDL_Log("Entering function %s", m_func);
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   270
#endif
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   271
    }
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   272
    ~LocalReferenceHolder() {
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   273
#ifdef DEBUG_JNI
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   274
        SDL_Log("Leaving function %s", m_func);
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   275
#endif
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   276
        if (m_env) {
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   277
            m_env->PopLocalFrame(NULL);
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   278
            --s_active;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   279
        }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   280
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   281
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   282
    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
   283
        if (env->PushLocalFrame(capacity) < 0) {
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   284
            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
   285
            return false;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   286
        }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   287
        ++s_active;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   288
        m_env = env;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   289
        return true;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   290
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   291
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   292
protected:
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   293
    JNIEnv *m_env;
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   294
    const char *m_func;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   295
};
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   296
int LocalReferenceHolder::s_active;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   297
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
   298
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
   299
                                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
   300
                                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
   301
                                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
   302
{
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
   303
    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
   304
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
   305
    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
   306
        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
   307
        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
   308
        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
   309
        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
   310
        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
   311
        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
   312
        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
   313
        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
   314
        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
   315
        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
   316
        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
   317
    };
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 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
   319
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
   320
    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
   321
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
    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
   323
    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
   324
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
    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
   326
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
    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
   328
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
    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
   330
}
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
   331
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
   332
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
   333
{
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
   334
    JNIEnv *mEnv = Android_JNI_GetEnv();
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   335
    mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers); 
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   336
}
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   337
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   338
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
   339
{
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   340
    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
   341
    JNIEnv *mEnv = Android_JNI_GetEnv();
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   342
    mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   343
    if (mid) {
6307
6048116f40b1 Fix for issue #1465, Leak in Android_JNI_SetActivityTitle (SDL_SetWindowTitle)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6284
diff changeset
   344
        jstring jtitle = reinterpret_cast<jstring>(mEnv->NewStringUTF(title));
6048116f40b1 Fix for issue #1465, Leak in Android_JNI_SetActivityTitle (SDL_SetWindowTitle)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6284
diff changeset
   345
        mEnv->CallStaticVoidMethod(mActivityClass, mid, jtitle);
6048116f40b1 Fix for issue #1465, Leak in Android_JNI_SetActivityTitle (SDL_SetWindowTitle)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6284
diff changeset
   346
        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
   347
    }
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
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   350
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
   351
{
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   352
    int i;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   353
    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
   354
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   355
    if (bHasNewData) {
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   356
        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
   357
            values[i] = fLastAccelerometer[i];
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   358
        }
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   359
        bHasNewData = false;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   360
        retval = SDL_TRUE;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   361
    }
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   362
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   363
    return retval;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   364
}
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   365
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
   366
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
   367
    /* 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
   368
    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
   369
    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
   370
        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
   371
        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
   372
    }
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   373
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   374
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   375
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
   376
    /* 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
   377
     * 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
   378
     * 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
   379
     * 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
   380
     * 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
   381
     * 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
   382
     * 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
   383
     * 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
   384
     * 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
   385
     * 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
   386
     */
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
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
    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
   389
    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
   390
    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
   391
        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
   392
        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
   393
    }
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
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
    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
   396
}
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
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
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
   399
    /* 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
   400
     * 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
   401
     * 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
   402
     * 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
   403
     * 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
   404
     * 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
   405
     * 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
   406
     *       (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
   407
     */
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
    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
   409
    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
   410
    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
   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
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   413
//
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   414
// Audio support
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   415
//
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   416
static jboolean audioBuffer16Bit = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   417
static jboolean audioBufferStereo = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   418
static jobject audioBuffer = NULL;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   419
static void* audioBufferPinned = NULL;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   420
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   421
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
   422
{
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   423
    int audioBufferFrames;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   424
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
   425
    int status;
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
   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
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
    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
   429
        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
   430
    }
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
   431
    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
   432
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   433
    
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   434
    __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
   435
    audioBuffer16Bit = is16Bit;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   436
    audioBufferStereo = channelCount > 1;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   437
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   438
    env->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   439
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   440
    /* 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
   441
     * Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   442
    
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   443
    if (is16Bit) {
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   444
        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
   445
        if (audioBufferLocal) {
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   446
            audioBuffer = env->NewGlobalRef(audioBufferLocal);
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   447
            env->DeleteLocalRef(audioBufferLocal);
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   448
        }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   449
    }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   450
    else {
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   451
        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
   452
        if (audioBufferLocal) {
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   453
            audioBuffer = env->NewGlobalRef(audioBufferLocal);
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   454
            env->DeleteLocalRef(audioBufferLocal);
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
    }
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   457
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   458
    if (audioBuffer == NULL) {
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   459
        __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
   460
        return 0;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   461
    }
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   462
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   463
    jboolean isCopy = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   464
    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
   465
        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
   466
        audioBufferFrames = env->GetArrayLength((jshortArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   467
    } 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
   468
        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
   469
        audioBufferFrames = env->GetArrayLength((jbyteArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   470
    }
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   471
    if (audioBufferStereo) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   472
        audioBufferFrames /= 2;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   473
    }
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
   474
 
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   475
    return audioBufferFrames;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   476
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   477
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   478
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
   479
{
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   480
    return audioBufferPinned;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   481
}
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
   482
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   483
extern "C" void Android_JNI_WriteAudioBuffer()
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   484
{
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
   485
    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
   486
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   487
    if (audioBuffer16Bit) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   488
        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
   489
        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
   490
    } else {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   491
        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
   492
        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
   493
    }
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
   494
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   495
    /* 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
   496
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   497
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   498
extern "C" void Android_JNI_CloseAudioDevice()
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   499
{
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
   500
    int status;
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 *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
   502
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   503
    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
   504
4997
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   505
    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
   506
        env->DeleteGlobalRef(audioBuffer);
4997
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   507
        audioBuffer = NULL;
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   508
        audioBufferPinned = NULL;
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
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
}
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
   511
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   512
// Test for an exception and call SDL_SetError with its detail if one occurs
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   513
static bool Android_JNI_ExceptionOccurred()
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   514
{
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   515
    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
   516
    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
   517
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   518
    jthrowable exception = mEnv->ExceptionOccurred();
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   519
    if (exception != NULL) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   520
        jmethodID mid;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   521
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   522
        // 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
   523
        mEnv->ExceptionClear();
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   524
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   525
        jclass exceptionClass = mEnv->GetObjectClass(exception);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   526
        jclass classClass = mEnv->FindClass("java/lang/Class");
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   527
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   528
        mid = mEnv->GetMethodID(classClass, "getName", "()Ljava/lang/String;");
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   529
        jstring exceptionName = (jstring)mEnv->CallObjectMethod(exceptionClass, mid);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   530
        const char* exceptionNameUTF8 = mEnv->GetStringUTFChars(exceptionName, 0);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   531
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   532
        mid = mEnv->GetMethodID(exceptionClass, "getMessage", "()Ljava/lang/String;");
5860
b89f7f3bc9be Called method on wrong object in Android exception handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 5650
diff changeset
   533
        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
   534
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   535
        if (exceptionMessage != NULL) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   536
            const char* exceptionMessageUTF8 = mEnv->GetStringUTFChars(
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   537
                    exceptionMessage, 0);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   538
            SDL_SetError("%s: %s", exceptionNameUTF8, exceptionMessageUTF8);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   539
            mEnv->ReleaseStringUTFChars(exceptionMessage, exceptionMessageUTF8);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   540
        } else {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   541
            SDL_SetError("%s", exceptionNameUTF8);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   542
        }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   543
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   544
        mEnv->ReleaseStringUTFChars(exceptionName, exceptionNameUTF8);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   545
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   546
        return true;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   547
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   548
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   549
    return false;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   550
}
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   551
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   552
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
   553
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   554
    LocalReferenceHolder refs(__FUNCTION__);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   555
    int result = 0;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   556
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   557
    jmethodID mid;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   558
    jobject context;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   559
    jobject assetManager;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   560
    jobject inputStream;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   561
    jclass channels;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   562
    jobject readableByteChannel;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   563
    jstring fileNameJString;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   564
    jobject fd;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   565
    jclass fdCls;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   566
    jfieldID descriptor;
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   567
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
   568
    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
   569
    if (!refs.init(mEnv)) {
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   570
        goto failure;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   571
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   572
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
   573
    fileNameJString = (jstring)ctx->hidden.androidio.fileNameRef;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   574
    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
   575
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   576
    // context = SDLActivity.getContext();
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   577
    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
   578
            "getContext","()Landroid/content/Context;");
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   579
    context = mEnv->CallStaticObjectMethod(mActivityClass, mid);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   580
    
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   581
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   582
    // 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
   583
    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
   584
            "getAssets", "()Landroid/content/res/AssetManager;");
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   585
    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
   586
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   587
    /* 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
   588
    * 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
   589
    */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   590
    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
   591
    inputStream = mEnv->CallObjectMethod(assetManager, mid, fileNameJString);
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   592
    if (Android_JNI_ExceptionOccurred()) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   593
        goto fallback;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   594
    }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   595
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   596
    ctx->hidden.androidio.assetFileDescriptorRef = mEnv->NewGlobalRef(inputStream);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   597
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream), "getStartOffset", "()J");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   598
    ctx->hidden.androidio.offset = mEnv->CallLongMethod(inputStream, mid);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   599
    if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   600
        goto fallback;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   601
    }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   602
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   603
    mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream), "getDeclaredLength", "()J");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   604
    ctx->hidden.androidio.size = mEnv->CallLongMethod(inputStream, mid);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   605
    
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   606
    if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   607
        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
   608
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   609
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   610
    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
   611
    fd = mEnv->CallObjectMethod(inputStream, mid);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   612
    fdCls = mEnv->GetObjectClass(fd);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   613
    descriptor = mEnv->GetFieldID(fdCls, "descriptor", "I");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   614
    ctx->hidden.androidio.fd = mEnv->GetIntField(fd, descriptor);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   615
6816
b3d3ef1e15b5 Fixes bug 1696, thanks Paul Geerts!
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6806
diff changeset
   616
    // Seek to the correct offset in the file.
b3d3ef1e15b5 Fixes bug 1696, thanks Paul Geerts!
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6806
diff changeset
   617
    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
   618
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   619
    if (false) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   620
fallback:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   621
        __android_log_print(ANDROID_LOG_DEBUG, "SDL", "Falling back to legacy InputStream method for opening file");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   622
        /* Try the old method using InputStream */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   623
        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
   624
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   625
        // inputStream = assetManager.open(<filename>);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   626
        mid = mEnv->GetMethodID(mEnv->GetObjectClass(assetManager),
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   627
                "open", "(Ljava/lang/String;I)Ljava/io/InputStream;");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   628
        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
   629
        if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   630
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   631
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   632
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   633
        ctx->hidden.androidio.inputStreamRef = mEnv->NewGlobalRef(inputStream);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   634
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   635
        // 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
   636
        // 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
   637
        // 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
   638
        // 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
   639
        // 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
   640
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   641
        // size = inputStream.available();
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   642
        mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   643
                "available", "()I");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   644
        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
   645
        if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   646
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   647
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   648
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   649
        // readableByteChannel = Channels.newChannel(inputStream);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   650
        channels = mEnv->FindClass("java/nio/channels/Channels");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   651
        mid = mEnv->GetStaticMethodID(channels,
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   652
                "newChannel",
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   653
                "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   654
        readableByteChannel = mEnv->CallStaticObjectMethod(
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   655
                channels, mid, inputStream);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   656
        if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   657
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   658
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   659
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   660
        ctx->hidden.androidio.readableByteChannelRef =
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   661
            mEnv->NewGlobalRef(readableByteChannel);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   662
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   663
        // Store .read id for reading purposes
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   664
        mid = mEnv->GetMethodID(mEnv->GetObjectClass(readableByteChannel),
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   665
                "read", "(Ljava/nio/ByteBuffer;)I");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   666
        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
   667
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   668
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   669
    if (false) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   670
failure:
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   671
        result = -1;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   672
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   673
        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
   674
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   675
        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
   676
            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
   677
        }
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
   678
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   679
        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
   680
            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
   681
        }
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   682
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   683
        if(ctx->hidden.androidio.assetFileDescriptorRef != NULL) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   684
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.assetFileDescriptorRef);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   685
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   686
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   687
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   688
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   689
    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
   690
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   691
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   692
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
   693
        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
   694
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   695
    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
   696
    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
   697
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   698
    if (!refs.init(mEnv)) {
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   699
        return -1;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   700
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   701
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   702
    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
   703
        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
   704
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   705
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   706
    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
   707
    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
   708
    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
   709
    ctx->hidden.androidio.readableByteChannelRef = NULL;
fbb84f5b985f Fixes #1519, adds initialization to Android RWops internal variables.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6330
diff changeset
   710
    ctx->hidden.androidio.readMethod = NULL;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   711
    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
   712
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   713
    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
   714
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   715
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   716
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
   717
        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
   718
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   719
    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
   720
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   721
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   722
        size_t bytesMax = size * maxnum;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   723
        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
   724
            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
   725
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   726
        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
   727
        if (result > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   728
            ctx->hidden.androidio.position += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   729
            return result / size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   730
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   731
        return 0;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   732
    } else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   733
        jlong bytesRemaining = (jlong) (size * maxnum);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   734
        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
   735
        int bytesRead = 0;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   736
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   737
        /* 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
   738
        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
   739
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   740
        JNIEnv *mEnv = Android_JNI_GetEnv();
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   741
        if (!refs.init(mEnv)) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   742
            return -1;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   743
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   744
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   745
        jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannelRef;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   746
        jmethodID readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   747
        jobject byteBuffer = mEnv->NewDirectByteBuffer(buffer, bytesRemaining);
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
        while (bytesRemaining > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   750
            // result = readableByteChannel.read(...);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   751
            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
   752
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   753
            if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   754
                return 0;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   755
            }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   756
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   757
            if (result < 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   758
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   759
            }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   760
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   761
            bytesRemaining -= result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   762
            bytesRead += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   763
            ctx->hidden.androidio.position += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   764
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   765
        return bytesRead / size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   766
    }    
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   767
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   768
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   769
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
   770
        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
   771
{
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   772
    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
   773
    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
   774
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   775
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   776
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
   777
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   778
    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
   779
    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
   780
    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
   781
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   782
    if (!refs.init(mEnv)) {
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   783
        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
   784
        return -1;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   785
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   786
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   787
    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
   788
        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
   789
            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
   790
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   791
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   792
        if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   793
            jobject inputStream = (jobject)ctx->hidden.androidio.assetFileDescriptorRef;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   794
            jmethodID mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   795
                    "close", "()V");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   796
            mEnv->CallVoidMethod(inputStream, mid);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   797
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.assetFileDescriptorRef);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   798
            if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   799
                result = -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   800
            }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   801
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   802
        else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   803
            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
   804
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   805
            // inputStream.close();
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   806
            jmethodID mid = mEnv->GetMethodID(mEnv->GetObjectClass(inputStream),
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   807
                    "close", "()V");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   808
            mEnv->CallVoidMethod(inputStream, mid);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   809
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.inputStreamRef);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   810
            mEnv->DeleteGlobalRef((jobject)ctx->hidden.androidio.readableByteChannelRef);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   811
            if (Android_JNI_ExceptionOccurred()) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   812
                result = -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   813
            }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   814
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   815
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   816
        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
   817
            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
   818
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   819
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   820
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   821
    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
   822
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   823
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   824
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   825
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
   826
{
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   827
    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
   828
}
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   829
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   830
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
   831
{
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   832
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   833
        switch (whence) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   834
            case RW_SEEK_SET:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   835
                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
   836
                offset += ctx->hidden.androidio.offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   837
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   838
            case RW_SEEK_CUR:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   839
                offset += ctx->hidden.androidio.position;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   840
                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
   841
                offset += ctx->hidden.androidio.offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   842
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   843
            case RW_SEEK_END:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   844
                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
   845
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   846
            default:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   847
                SDL_SetError("Unknown value for 'whence'");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   848
                return -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   849
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   850
        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
   851
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   852
        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
   853
        if (ret == -1) return -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   854
        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
   855
    } else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   856
        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
   857
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   858
        switch (whence) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   859
            case RW_SEEK_SET:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   860
                newPosition = offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   861
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   862
            case RW_SEEK_CUR:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   863
                newPosition = ctx->hidden.androidio.position + offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   864
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   865
            case RW_SEEK_END:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   866
                newPosition = ctx->hidden.androidio.size + offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   867
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   868
            default:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   869
                SDL_SetError("Unknown value for 'whence'");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   870
                return -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   871
        }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   872
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   873
        /* Validate the new position */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   874
        if (newPosition < 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   875
            SDL_Error(SDL_EFSEEK);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   876
            return -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   877
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   878
        if (newPosition > ctx->hidden.androidio.size) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   879
            newPosition = ctx->hidden.androidio.size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   880
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   881
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   882
        Sint64 movement = newPosition - ctx->hidden.androidio.position;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   883
        if (movement > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   884
            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
   885
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   886
            // The easy case where we're seeking forwards
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   887
            while (movement > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   888
                Sint64 amount = sizeof (buffer);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   889
                if (amount > movement) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   890
                    amount = movement;
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
                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
   893
                if (result <= 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   894
                    // 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
   895
                    return -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   896
                }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   897
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   898
                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
   899
            }
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
   900
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   901
        } else if (movement < 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   902
            // 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
   903
            // forwards which obviously isn't very efficient
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   904
            Android_JNI_FileClose(ctx, false);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   905
            Android_JNI_FileOpen(ctx);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   906
            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
   907
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   908
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   909
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   910
    return ctx->hidden.androidio.position;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   911
    
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   912
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   913
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   914
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
   915
{
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   916
    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
   917
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   918
6464
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   919
// 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
   920
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
   921
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   922
    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
   923
    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
   924
    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
   925
        return NULL;
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   926
    }
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   927
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   928
    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
   929
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   930
    jmethodID mid;
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   931
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   932
    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
   933
    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
   934
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   935
    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
   936
    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
   937
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   938
    env->DeleteLocalRef(service);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   939
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   940
    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
   941
}
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   942
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   943
#define SETUP_CLIPBOARD(error) \
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   944
    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
   945
    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
   946
    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
   947
        return error; \
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
    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
   950
    if (!clipboard) { \
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   951
        return error; \
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   952
    }
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
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
   955
{
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   956
    SETUP_CLIPBOARD(-1)
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   957
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   958
    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
   959
    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
   960
    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
   961
    env->DeleteGlobalRef(clipboard);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   962
    env->DeleteLocalRef(string);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   963
    return 0;
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   964
}
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   965
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   966
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
   967
{
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   968
    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
   969
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   970
    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
   971
    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
   972
    env->DeleteGlobalRef(clipboard);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   973
    if (sequence) {
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   974
        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
   975
        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
   976
        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
   977
        if (utf) {
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   978
            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
   979
            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
   980
            return text;
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
    }
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   983
    return SDL_strdup("");
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   984
}
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   985
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   986
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
   987
{
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   988
    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
   989
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   990
    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
   991
    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
   992
    env->DeleteGlobalRef(clipboard);
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   993
    return has ? SDL_TRUE : SDL_FALSE;
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   994
}
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
6448
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
   997
// returns 0 on success or -1 on error (others undefined then)
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
   998
// returns truthy or falsy value in plugged, charged and battery
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
   999
// returns the value in seconds and percent or -1 if not available
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1000
extern "C" int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent)
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1001
{
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
  1002
    LocalReferenceHolder refs(__FUNCTION__);
6448
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1003
    JNIEnv* env = Android_JNI_GetEnv();
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1004
    if (!refs.init(env)) {
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1005
        return -1;
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1006
    }
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1007
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1008
    jmethodID mid;
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1009
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1010
    mid = env->GetStaticMethodID(mActivityClass, "getContext", "()Landroid/content/Context;");
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1011
    jobject context = env->CallStaticObjectMethod(mActivityClass, mid);
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
  1012
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lanti