src/core/android/SDL_android.c
author Eric Wing <ewing . public |-at-| gmail . com>
Mon, 22 Jul 2013 02:51:45 -0700
changeset 7501 b27825bb5879
parent 7368 src/core/android/SDL_android.cpp@0e12e09df086
child 7514 a9008fa2548a
permissions -rw-r--r--
Android: Removed all unnecessary dependencies on C++. C++ is a bit of a minefield on Android. Much functionality still doesn't work, and Android can't decide on which C++ standard library to use, so it provides 3 different ones, all of which are incompatible with each other. (It looks like clang is coming too which will add a new compiler and a 4th standard library.) As middleware, SDL might be distributed as a binary and intermixed with other projects already using C++. If C++ is intermixed in a bad way, bad things will happen. Removing dependencies on C++ will avoid this problem and downstream users won't have to worry/care.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6864
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
5222
c66b2a778b7e Try to create an OpenGL ES 2.0 context on Android and successfully fall back to OpenGL ES 1.1 if that fails.
Sam Lantinga <slouken@libsdl.org>
parents: 5092
diff changeset
    22
#include "SDL_stdinc.h"
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
    23
#include "SDL_assert.h"
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
    24
#include "SDL_log.h"
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5996
diff changeset
    26
#ifdef __ANDROID__
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5996
diff changeset
    27
6630
55910871076b Added some platform specific API functions for Android:
Sam Lantinga <slouken@libsdl.org>
parents: 6555
diff changeset
    28
#include "SDL_system.h"
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
    29
#include "SDL_android.h"
6792
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
    30
#include <EGL/egl.h>
4989
58b6bb4a45e9 More Android cleanup:
Sam Lantinga <slouken@libsdl.org>
parents: 4981
diff changeset
    31
5092
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    32
#include "../../events/SDL_events_c.h"
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    33
#include "../../video/android/SDL_androidkeyboard.h"
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    34
#include "../../video/android/SDL_androidtouch.h"
ed1d54f1290a Fixed Android build
Sam Lantinga <slouken@libsdl.org>
parents: 5090
diff changeset
    35
#include "../../video/android/SDL_androidvideo.h"
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    36
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
    37
#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
    38
#include <pthread.h>
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
    39
#include <sys/types.h>
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
    40
#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
    41
#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
    42
//#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
    43
//#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
    44
#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
    45
#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
    46
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
    47
/* 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
    48
//#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
    49
6191
2c0d35b1af4e Made the application activity events consistent between iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 6189
diff changeset
    50
/* 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
    51
extern void Android_RunAudioThread();
7246
be3beed886ea Moved prototype for static function out of header file to prevent warnings.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
    52
be3beed886ea Moved prototype for static function out of header file to prevent warnings.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
    53
static void Android_JNI_ThreadDestroyed(void*);
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>
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
    60
#include <stdbool.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
    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
/*******************************************************************************
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
                               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
    65
*******************************************************************************/
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
    66
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
    67
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
    68
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    69
// 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
    70
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
    71
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    72
// method signatures
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    73
static jmethodID midCreateGLContext;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    74
static jmethodID midFlipBuffers;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    75
static jmethodID midAudioInit;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    76
static jmethodID midAudioWriteShortBuffer;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    77
static jmethodID midAudioWriteByteBuffer;
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
    78
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
    79
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    80
// Accelerometer data storage
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
    81
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
    82
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
    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
/*******************************************************************************
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
                 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
    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
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
// Library init
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
    89
jint JNI_OnLoad(JavaVM* vm, void* reserved)
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
    90
{
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
    91
    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
    92
    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
    93
    LOGI("JNI_OnLoad called");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
    94
    if ((*mJavaVM)->GetEnv(mJavaVM, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
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
    95
        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
    96
        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
    97
    }
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
    98
    /*
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
     * 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
   100
     * 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
   101
     */
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
    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
   103
        __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
   104
    }
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
    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
   106
        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
   107
    }
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
   108
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
   109
    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
   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
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
// Called before SDL_main() to initialize JNI bindings
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   113
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
   114
{
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
    __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
   116
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
   117
    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
   118
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   119
    mActivityClass = (jclass)((*mEnv)->NewGlobalRef(mEnv, cls));
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   120
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   121
    midCreateGLContext = (*mEnv)->GetStaticMethodID(mEnv, 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
   122
                                "createGLContext","(II[I)Z");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   123
    midFlipBuffers = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   124
                                "flipBuffers","()V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   125
    midAudioInit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   126
                                "audioInit", "(IZZI)V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   127
    midAudioWriteShortBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   128
                                "audioWriteShortBuffer", "([S)V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   129
    midAudioWriteByteBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   130
                                "audioWriteByteBuffer", "([B)V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   131
    midAudioQuit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   132
                                "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
   133
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   134
    bHasNewData = false;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   135
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   136
    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   137
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   138
        __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
   139
    }
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
   140
    __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
   141
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   143
// Resize
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   144
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
   145
                                    JNIEnv* env, jclass jcls,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   146
                                    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
   147
{
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   148
    Android_SetScreenResolution(width, height, format);
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
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   151
// Keydown
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   152
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
   153
                                    JNIEnv* env, jclass jcls, jint keycode)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   154
{
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
   155
    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
   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
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
// Keyup
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   159
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
   160
                                    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
   161
{
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
   162
    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
   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
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
// Touch
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   166
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
   167
                                    JNIEnv* env, jclass jcls,
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5860
diff changeset
   168
                                    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
   169
                                    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
   170
{
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5860
diff changeset
   171
    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
   172
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   174
// Accelerometer
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   175
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
   176
                                    JNIEnv* env, jclass jcls,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   177
                                    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
   178
{
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[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
   180
    fLastAccelerometer[1] = y;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   181
    fLastAccelerometer[2] = z;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   182
    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
   183
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   185
// Low memory
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   186
void Java_org_libsdl_app_SDLActivity_nativeLowMemory(
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   187
                                    JNIEnv* env, jclass cls)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   188
{
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   189
    SDL_SendAppEvent(SDL_APP_LOWMEMORY);
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   190
}
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   191
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   192
// Quit
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   193
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
   194
                                    JNIEnv* env, jclass cls)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   195
{
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   196
    // Inject a SDL_QUIT event
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   197
    SDL_SendQuit();
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   198
    SDL_SendAppEvent(SDL_APP_TERMINATING);
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   199
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   200
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   201
// Pause
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   202
void Java_org_libsdl_app_SDLActivity_nativePause(
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   203
                                    JNIEnv* env, jclass cls)
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   204
{
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   205
    if (Android_Window) {
6330
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   206
        /* 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
   207
        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
   208
        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
   209
        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
   210
    }
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   211
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   212
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   213
    SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   214
    SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   215
}
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
// Resume
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   218
void Java_org_libsdl_app_SDLActivity_nativeResume(
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   219
                                    JNIEnv* env, jclass cls)
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   220
{
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   221
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   222
    SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   223
    SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   224
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   225
    if (Android_Window) {
6330
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   226
        /* 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
   227
         * 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
   228
         * 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
   229
         */
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   230
        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
   231
        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
   232
        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
   233
    }
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   234
}
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   235
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   236
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
   237
                                    JNIEnv* env, jclass cls)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   238
{
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   239
    /* 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
   240
    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
   241
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   242
    Android_RunAudioThread();
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   243
}
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
   244
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   245
void Java_org_libsdl_app_SDLInputConnection_nativeCommitText(
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   246
                                    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
   247
                                    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
   248
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   249
    const char *utftext = (*env)->GetStringUTFChars(env, text, NULL);
6555
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
    SDL_SendKeyboardText(utftext);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   252
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   253
    (*env)->ReleaseStringUTFChars(env, text, utftext);
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   254
}
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   255
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   256
void Java_org_libsdl_app_SDLInputConnection_nativeSetComposingText(
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   257
                                    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
   258
                                    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
   259
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   260
    const char *utftext = (*env)->GetStringUTFChars(env, text, NULL);
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   261
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   262
    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
   263
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   264
    (*env)->ReleaseStringUTFChars(env, text, utftext);
6555
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   265
}
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   266
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   267
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   268
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
   269
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
             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
   271
*******************************************************************************/
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   272
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   273
static int s_active = 0;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   274
struct LocalReferenceHolder
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   275
{
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   276
    JNIEnv *m_env;
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   277
    const char *m_func;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   278
};
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   279
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   280
static struct LocalReferenceHolder LocalReferenceHolder_Setup(const char *func)
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   281
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   282
    struct LocalReferenceHolder refholder;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   283
    refholder.m_env = NULL;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   284
    refholder.m_func = func;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   285
#ifdef DEBUG_JNI
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   286
    SDL_Log("Entering function %s", func);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   287
#endif
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   288
    return refholder;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   289
}
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   290
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   291
static SDL_bool LocalReferenceHolder_Init(struct LocalReferenceHolder *refholder, JNIEnv *env)
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   292
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   293
    const int capacity = 16;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   294
    if ((*env)->PushLocalFrame(env, capacity) < 0) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   295
        SDL_SetError("Failed to allocate enough JVM local references");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   296
        return false;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   297
    }
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   298
    ++s_active;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   299
    refholder->m_env = env;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   300
    return SDL_TRUE;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   301
}
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   302
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   303
static void LocalReferenceHolder_Cleanup(struct LocalReferenceHolder *refholder)
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   304
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   305
#ifdef DEBUG_JNI
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   306
    SDL_Log("Leaving function %s", refholder->m_func);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   307
#endif
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   308
    if (refholder->m_env) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   309
        JNIEnv* env = refholder->m_env;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   310
        (*env)->PopLocalFrame(env, NULL);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   311
        --s_active;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   312
    }
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   313
}
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   314
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   315
static SDL_bool LocalReferenceHolder_IsActive()
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   316
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   317
    return s_active > 0;    
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   318
}
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   319
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   320
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   321
SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion,
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
   322
                                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
   323
                                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
   324
                                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
   325
{
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
   326
    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
   327
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
    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
   329
        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
   330
        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
   331
        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
   332
        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
   333
        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
   334
        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
   335
        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
   336
        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
   337
        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
   338
        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
   339
        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
   340
    };
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   341
    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
   342
a15821a3471f Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
   343
    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
   344
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   345
    array = (*env)->NewIntArray(env, len);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   346
    (*env)->SetIntArrayRegion(env, array, 0, len, attribs);
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
   347
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   348
    jboolean success = (*env)->CallStaticBooleanMethod(env, mActivityClass, midCreateGLContext, majorVersion, minorVersion, array);
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
   349
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   350
    (*env)->DeleteLocalRef(env, array);
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
   351
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
   352
    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
   353
}
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
   354
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   355
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
   356
{
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
   357
    JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   358
    (*mEnv)->CallStaticVoidMethod(mEnv, mActivityClass, midFlipBuffers);
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   359
}
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   360
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   361
void Android_JNI_SetActivityTitle(const char *title)
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   362
{
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   363
    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
   364
    JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   365
    mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,"setActivityTitle","(Ljava/lang/String;)Z");
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   366
    if (mid) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   367
        jstring jtitle = (jstring)((*mEnv)->NewStringUTF(mEnv, title));
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   368
        (*mEnv)->CallStaticBooleanMethod(mEnv, mActivityClass, mid, jtitle);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   369
        (*mEnv)->DeleteLocalRef(mEnv, jtitle);
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   370
    }
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
   371
}
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
   372
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   373
SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   374
{
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   375
    int i;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   376
    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
   377
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   378
    if (bHasNewData) {
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   379
        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
   380
            values[i] = fLastAccelerometer[i];
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   381
        }
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   382
        bHasNewData = false;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   383
        retval = SDL_TRUE;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   384
    }
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   385
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   386
    return retval;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   387
}
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   388
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
   389
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
   390
    /* 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
   391
    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
   392
    if (env != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   393
        (*mJavaVM)->DetachCurrentThread(mJavaVM);
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
   394
        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
   395
    }
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
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
   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
     * 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
   401
     * 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
   402
     * 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
   403
     * 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
   404
     * 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
   405
     * 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
   406
     * 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
   407
     * 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
   408
     * 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
   409
     */
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
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
    JNIEnv *env;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   412
    int status = (*mJavaVM)->AttachCurrentThread(mJavaVM, &env, NULL);
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
   413
    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
   414
        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
   415
        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
   416
    }
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   417
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   418
    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
   419
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   420
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   421
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
   422
    /* 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
   423
     * 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
   424
     * 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
   425
     * 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
   426
     * 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
   427
     * 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
   428
     * 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
   429
     *       (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
   430
     */
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
    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
   432
    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
   433
    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
   434
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   435
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   436
//
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   437
// Audio support
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   438
//
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   439
static jboolean audioBuffer16Bit = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   440
static jboolean audioBufferStereo = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   441
static jobject audioBuffer = NULL;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   442
static void* audioBufferPinned = NULL;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   443
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   444
int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   445
{
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   446
    int audioBufferFrames;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   447
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   448
    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
   449
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   450
    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
   451
        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
   452
    }
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
   453
    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
   454
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   455
    __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
   456
    audioBuffer16Bit = is16Bit;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   457
    audioBufferStereo = channelCount > 1;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   458
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   459
    (*env)->CallStaticVoidMethod(env, mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   460
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   461
    /* 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
   462
     * Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   463
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   464
    if (is16Bit) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   465
        jshortArray audioBufferLocal = (*env)->NewShortArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   466
        if (audioBufferLocal) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   467
            audioBuffer = (*env)->NewGlobalRef(env, audioBufferLocal);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   468
            (*env)->DeleteLocalRef(env, audioBufferLocal);
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   469
        }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   470
    }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   471
    else {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   472
        jbyteArray audioBufferLocal = (*env)->NewByteArray(env, desiredBufferFrames * (audioBufferStereo ? 2 : 1));
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   473
        if (audioBufferLocal) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   474
            audioBuffer = (*env)->NewGlobalRef(env, audioBufferLocal);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   475
            (*env)->DeleteLocalRef(env, audioBufferLocal);
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   476
        }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   477
    }
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   478
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   479
    if (audioBuffer == NULL) {
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   480
        __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
   481
        return 0;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   482
    }
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   483
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   484
    jboolean isCopy = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   485
    if (audioBuffer16Bit) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   486
        audioBufferPinned = (*env)->GetShortArrayElements(env, (jshortArray)audioBuffer, &isCopy);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   487
        audioBufferFrames = (*env)->GetArrayLength(env, (jshortArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   488
    } else {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   489
        audioBufferPinned = (*env)->GetByteArrayElements(env, (jbyteArray)audioBuffer, &isCopy);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   490
        audioBufferFrames = (*env)->GetArrayLength(env, (jbyteArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   491
    }
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   492
    if (audioBufferStereo) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   493
        audioBufferFrames /= 2;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   494
    }
6864
97187387ad79 Bug 1703 - SDL calls a wrong JNI function on Android by Philipp Wiesemann
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6828
diff changeset
   495
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   496
    return audioBufferFrames;
4995
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
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   499
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
   500
{
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   501
    return audioBufferPinned;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   502
}
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
   503
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   504
void Android_JNI_WriteAudioBuffer()
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   505
{
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
   506
    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
   507
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   508
    if (audioBuffer16Bit) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   509
        (*mAudioEnv)->ReleaseShortArrayElements(mAudioEnv, (jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   510
        (*mAudioEnv)->CallStaticVoidMethod(mAudioEnv, mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   511
    } else {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   512
        (*mAudioEnv)->ReleaseByteArrayElements(mAudioEnv, (jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   513
        (*mAudioEnv)->CallStaticVoidMethod(mAudioEnv, mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   514
    }
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
   515
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   516
    /* 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
   517
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   518
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   519
void Android_JNI_CloseAudioDevice()
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   520
{
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
   521
    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
   522
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   523
    (*env)->CallStaticVoidMethod(env, 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
   524
4997
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   525
    if (audioBuffer) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   526
        (*env)->DeleteGlobalRef(env, audioBuffer);
4997
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   527
        audioBuffer = NULL;
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   528
        audioBufferPinned = NULL;
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   529
    }
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
   530
}
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
   531
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   532
// Test for an exception and call SDL_SetError with its detail if one occurs
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   533
// If optional parameter silent is truthy then SDL_SetError() is not called.
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   534
static bool Android_JNI_ExceptionOccurred(bool silent)
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   535
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   536
    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
   537
    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
   538
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   539
    jthrowable exception = (*mEnv)->ExceptionOccurred(mEnv);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   540
    if (exception != NULL) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   541
        jmethodID mid;
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
        // Until this happens most JNI operations have undefined behaviour
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   544
        (*mEnv)->ExceptionClear(mEnv);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   545
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   546
        if (!silent) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   547
            jclass exceptionClass = (*mEnv)->GetObjectClass(mEnv, exception);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   548
            jclass classClass = (*mEnv)->FindClass(mEnv, "java/lang/Class");
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   549
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   550
            mid = (*mEnv)->GetMethodID(mEnv, classClass, "getName", "()Ljava/lang/String;");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   551
            jstring exceptionName = (jstring)(*mEnv)->CallObjectMethod(mEnv, exceptionClass, mid);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   552
            const char* exceptionNameUTF8 = (*mEnv)->GetStringUTFChars(mEnv, exceptionName, 0);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   553
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   554
            mid = (*mEnv)->GetMethodID(mEnv, exceptionClass, "getMessage", "()Ljava/lang/String;");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   555
            jstring exceptionMessage = (jstring)(*mEnv)->CallObjectMethod(mEnv, exception, mid);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   556
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   557
            if (exceptionMessage != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   558
                const char* exceptionMessageUTF8 = (*mEnv)->GetStringUTFChars(mEnv, exceptionMessage, 0);
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   559
                SDL_SetError("%s: %s", exceptionNameUTF8, exceptionMessageUTF8);
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   560
                (*mEnv)->ReleaseStringUTFChars(mEnv, exceptionMessage, exceptionMessageUTF8);
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   561
            } else {
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   562
                SDL_SetError("%s", exceptionNameUTF8);
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   563
            }
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   564
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   565
            (*mEnv)->ReleaseStringUTFChars(mEnv, exceptionName, exceptionNameUTF8);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   566
        }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   567
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   568
        return true;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   569
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   570
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   571
    return false;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   572
}
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   573
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   574
static int Internal_Android_JNI_FileOpen(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
   575
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   576
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   577
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   578
    int result = 0;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   579
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   580
    jmethodID mid;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   581
    jobject context;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   582
    jobject assetManager;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   583
    jobject inputStream;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   584
    jclass channels;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   585
    jobject readableByteChannel;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   586
    jstring fileNameJString;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   587
    jobject fd;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   588
    jclass fdCls;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   589
    jfieldID descriptor;
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   590
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
   591
    JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   592
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   593
        goto failure;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   594
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   595
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
   596
    fileNameJString = (jstring)ctx->hidden.androidio.fileNameRef;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   597
    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
   598
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   599
    // context = SDLActivity.getContext();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   600
    mid = (*mEnv)->GetStaticMethodID(mEnv, 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
   601
            "getContext","()Landroid/content/Context;");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   602
    context = (*mEnv)->CallStaticObjectMethod(mEnv, mActivityClass, mid);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   603
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   604
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   605
    // assetManager = context.getAssets();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   606
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, context),
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   607
            "getAssets", "()Landroid/content/res/AssetManager;");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   608
    assetManager = (*mEnv)->CallObjectMethod(mEnv, 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
   609
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   610
    /* 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
   611
    * 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
   612
    */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   613
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, assetManager), "openFd", "(Ljava/lang/String;)Landroid/content/res/AssetFileDescriptor;");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   614
    inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString);
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   615
    if (Android_JNI_ExceptionOccurred(true)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   616
        goto fallback;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   617
    }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   618
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   619
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getStartOffset", "()J");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   620
    ctx->hidden.androidio.offset = (*mEnv)->CallLongMethod(mEnv, inputStream, mid);
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   621
    if (Android_JNI_ExceptionOccurred(true)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   622
        goto fallback;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   623
    }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   624
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   625
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getDeclaredLength", "()J");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   626
    ctx->hidden.androidio.size = (*mEnv)->CallLongMethod(mEnv, inputStream, mid);
7045
4be2c24b7de1 Fixed bug 1780 - SDL_RWFromFile() sets an error on Android although a valid SDL_RWops pointer is returned.
Sam Lantinga <slouken@libsdl.org>
parents: 7039
diff changeset
   627
    if (Android_JNI_ExceptionOccurred(true)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   628
        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
   629
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   630
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   631
    mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream), "getFileDescriptor", "()Ljava/io/FileDescriptor;");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   632
    fd = (*mEnv)->CallObjectMethod(mEnv, inputStream, mid);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   633
    fdCls = (*mEnv)->GetObjectClass(mEnv, fd);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   634
    descriptor = (*mEnv)->GetFieldID(mEnv, fdCls, "descriptor", "I");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   635
    ctx->hidden.androidio.fd = (*mEnv)->GetIntField(mEnv, fd, descriptor);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   636
    ctx->hidden.androidio.assetFileDescriptorRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   637
6816
b3d3ef1e15b5 Fixes bug 1696, thanks Paul Geerts!
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6806
diff changeset
   638
    // Seek to the correct offset in the file.
b3d3ef1e15b5 Fixes bug 1696, thanks Paul Geerts!
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6806
diff changeset
   639
    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
   640
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   641
    if (false) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   642
fallback:
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   643
        // Disabled log message because of spam on the Nexus 7
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   644
        //__android_log_print(ANDROID_LOG_DEBUG, "SDL", "Falling back to legacy InputStream method for opening file");
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   645
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   646
        /* Try the old method using InputStream */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   647
        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
   648
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   649
        // inputStream = assetManager.open(<filename>);
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   650
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, assetManager),
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   651
                "open", "(Ljava/lang/String;I)Ljava/io/InputStream;");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   652
        inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString, 1 /*ACCESS_RANDOM*/);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   653
        if (Android_JNI_ExceptionOccurred(false)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   654
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   655
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   656
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   657
        ctx->hidden.androidio.inputStreamRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);
6806
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
        // 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
   660
        // 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
   661
        // 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
   662
        // 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
   663
        // 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
   664
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   665
        // size = inputStream.available();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   666
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   667
                "available", "()I");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   668
        ctx->hidden.androidio.size = (long)(*mEnv)->CallIntMethod(mEnv, inputStream, mid);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   669
        if (Android_JNI_ExceptionOccurred(false)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   670
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   671
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   672
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   673
        // readableByteChannel = Channels.newChannel(inputStream);
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   674
        channels = (*mEnv)->FindClass(mEnv, "java/nio/channels/Channels");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   675
        mid = (*mEnv)->GetStaticMethodID(mEnv, channels,
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   676
                "newChannel",
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   677
                "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   678
        readableByteChannel = (*mEnv)->CallStaticObjectMethod(
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   679
                mEnv, channels, mid, inputStream);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   680
        if (Android_JNI_ExceptionOccurred(false)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   681
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   682
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   683
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   684
        ctx->hidden.androidio.readableByteChannelRef =
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   685
            (*mEnv)->NewGlobalRef(mEnv, readableByteChannel);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   686
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   687
        // Store .read id for reading purposes
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   688
        mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, readableByteChannel),
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   689
                "read", "(Ljava/nio/ByteBuffer;)I");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   690
        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
   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
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   693
    if (false) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   694
failure:
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   695
        result = -1;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   696
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   697
        (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.fileNameRef);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   698
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   699
        if(ctx->hidden.androidio.inputStreamRef != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   700
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.inputStreamRef);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   701
        }
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
   702
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   703
        if(ctx->hidden.androidio.readableByteChannelRef != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   704
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.readableByteChannelRef);
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
   705
        }
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   706
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   707
        if(ctx->hidden.androidio.assetFileDescriptorRef != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   708
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.assetFileDescriptorRef);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   709
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   710
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   711
    }
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   712
    
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   713
    LocalReferenceHolder_Cleanup(&refs);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   714
    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
   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
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   717
int Android_JNI_FileOpen(SDL_RWops* ctx,
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   718
        const char* fileName, const char* mode)
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   719
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   720
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__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
   721
    JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   722
    int retval;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   723
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   724
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   725
        LocalReferenceHolder_Cleanup(&refs);        
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   726
        return -1;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   727
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   728
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   729
    if (!ctx) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   730
        LocalReferenceHolder_Cleanup(&refs);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   731
        return -1;
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   732
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   733
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   734
    jstring fileNameJString = (*mEnv)->NewStringUTF(mEnv, fileName);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   735
    ctx->hidden.androidio.fileNameRef = (*mEnv)->NewGlobalRef(mEnv, fileNameJString);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   736
    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
   737
    ctx->hidden.androidio.readableByteChannelRef = NULL;
fbb84f5b985f Fixes #1519, adds initialization to Android RWops internal variables.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6330
diff changeset
   738
    ctx->hidden.androidio.readMethod = NULL;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   739
    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
   740
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   741
    retval = Internal_Android_JNI_FileOpen(ctx);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   742
    LocalReferenceHolder_Cleanup(&refs);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   743
    return retval;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   744
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   745
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   746
size_t Android_JNI_FileRead(SDL_RWops* ctx, void* buffer,
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   747
        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
   748
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   749
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__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
   750
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   751
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   752
        size_t bytesMax = size * maxnum;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   753
        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
   754
            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
   755
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   756
        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
   757
        if (result > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   758
            ctx->hidden.androidio.position += result;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   759
            LocalReferenceHolder_Cleanup(&refs);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   760
            return result / size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   761
        }
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   762
        LocalReferenceHolder_Cleanup(&refs);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   763
        return 0;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   764
    } else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   765
        jlong bytesRemaining = (jlong) (size * maxnum);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   766
        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
   767
        int bytesRead = 0;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   768
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   769
        /* 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
   770
        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
   771
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   772
        JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   773
        if (!LocalReferenceHolder_Init(&refs, mEnv)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   774
            LocalReferenceHolder_Cleanup(&refs);            
7368
0e12e09df086 Fixed SDL_RWread() returning -1 as unsigned instead of 0 if error on Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7246
diff changeset
   775
            return 0;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   776
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   777
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   778
        jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannelRef;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   779
        jmethodID readMethod = (jmethodID)ctx->hidden.androidio.readMethod;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   780
        jobject byteBuffer = (*mEnv)->NewDirectByteBuffer(mEnv, buffer, bytesRemaining);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   781
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   782
        while (bytesRemaining > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   783
            // result = readableByteChannel.read(...);
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   784
            int result = (*mEnv)->CallIntMethod(mEnv, 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
   785
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   786
            if (Android_JNI_ExceptionOccurred(false)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   787
                LocalReferenceHolder_Cleanup(&refs);            
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   788
                return 0;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   789
            }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   790
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   791
            if (result < 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   792
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   793
            }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   794
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   795
            bytesRemaining -= result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   796
            bytesRead += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   797
            ctx->hidden.androidio.position += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   798
        }
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   799
        LocalReferenceHolder_Cleanup(&refs);                    
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   800
        return bytesRead / size;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   801
    }
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   802
    LocalReferenceHolder_Cleanup(&refs);            
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   803
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   804
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   805
size_t Android_JNI_FileWrite(SDL_RWops* ctx, const void* buffer,
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   806
        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
   807
{
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   808
    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
   809
    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
   810
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   811
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   812
static int Internal_Android_JNI_FileClose(SDL_RWops* ctx, bool release)
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   813
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   814
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   815
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   816
    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
   817
    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
   818
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   819
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   820
        LocalReferenceHolder_Cleanup(&refs);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   821
        return SDL_SetError("Failed to allocate enough JVM local references");
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   822
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   823
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   824
    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
   825
        if (release) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   826
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.fileNameRef);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   827
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   828
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   829
        if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   830
            jobject inputStream = (jobject)ctx->hidden.androidio.assetFileDescriptorRef;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   831
            jmethodID mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   832
                    "close", "()V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   833
            (*mEnv)->CallVoidMethod(mEnv, inputStream, mid);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   834
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.assetFileDescriptorRef);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   835
            if (Android_JNI_ExceptionOccurred(false)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   836
                result = -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   837
            }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   838
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   839
        else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   840
            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
   841
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   842
            // inputStream.close();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   843
            jmethodID mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, inputStream),
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   844
                    "close", "()V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   845
            (*mEnv)->CallVoidMethod(mEnv, inputStream, mid);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   846
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.inputStreamRef);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   847
            (*mEnv)->DeleteGlobalRef(mEnv, (jobject)ctx->hidden.androidio.readableByteChannelRef);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   848
            if (Android_JNI_ExceptionOccurred(false)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   849
                result = -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   850
            }
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
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   852
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   853
        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
   854
            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
   855
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   856
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   857
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   858
    LocalReferenceHolder_Cleanup(&refs);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   859
    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
   860
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   861
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   862
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   863
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
   864
{
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   865
    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
   866
}
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   867
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   868
Sint64 Android_JNI_FileSeek(SDL_RWops* ctx, Sint64 offset, int whence)
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   869
{
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   870
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   871
        switch (whence) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   872
            case RW_SEEK_SET:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   873
                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
   874
                offset += ctx->hidden.androidio.offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   875
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   876
            case RW_SEEK_CUR:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   877
                offset += ctx->hidden.androidio.position;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   878
                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
   879
                offset += ctx->hidden.androidio.offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   880
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   881
            case RW_SEEK_END:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   882
                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
   883
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   884
            default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   885
                return SDL_SetError("Unknown value for 'whence'");
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   886
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   887
        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
   888
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   889
        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
   890
        if (ret == -1) return -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   891
        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
   892
    } else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   893
        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
   894
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   895
        switch (whence) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   896
            case RW_SEEK_SET:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   897
                newPosition = offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   898
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   899
            case RW_SEEK_CUR:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   900
                newPosition = ctx->hidden.androidio.position + offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   901
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   902
            case RW_SEEK_END:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   903
                newPosition = ctx->hidden.androidio.size + offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   904
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   905
            default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   906
                return SDL_SetError("Unknown value for 'whence'");
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   907
        }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   908
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   909
        /* Validate the new position */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   910
        if (newPosition < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   911
            return SDL_Error(SDL_EFSEEK);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   912
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   913
        if (newPosition > ctx->hidden.androidio.size) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   914
            newPosition = ctx->hidden.androidio.size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   915
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   916
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   917
        Sint64 movement = newPosition - ctx->hidden.androidio.position;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   918
        if (movement > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   919
            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
   920
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   921
            // The easy case where we're seeking forwards
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   922
            while (movement > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   923
                Sint64 amount = sizeof (buffer);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   924
                if (amount > movement) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   925
                    amount = movement;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   926
                }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   927
                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
   928
                if (result <= 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   929
                    // 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
   930
                    return -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   931
                }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   932
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   933
                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
   934
            }
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
   935
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   936
        } else if (movement < 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   937
            // 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
   938
            // forwards which obviously isn't very efficient
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   939
            Internal_Android_JNI_FileClose(ctx, false);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   940
            Internal_Android_JNI_FileOpen(ctx);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   941
            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
   942
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   943
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   944
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   945
    return ctx->hidden.androidio.position;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   946
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   947
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   948
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   949
int Android_JNI_FileClose(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
   950
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   951
    return Internal_Android_JNI_FileClose(ctx, true);
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   952
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   953
6464
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   954
// 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
   955
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
   956
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   957
    struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
6464
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   958
    JNIEnv* env = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   959
    jobject retval = NULL;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   960
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   961
    if (!LocalReferenceHolder_Init(&refs, env)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   962
        LocalReferenceHolder_Cleanup(&refs);
6464
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   963
        return NULL;
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
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   966
    jstring service = (*env)->NewStringUTF(env, name);
6464
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
    jmethodID mid;
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   969
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   970
    mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   971
    jobject context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   972
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   973
    mid = (*env)->GetMethodID(env, mActivityClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   974
    jobject manager = (*env)->CallObjectMethod(env, context, mid, service);
6464
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   975
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   976
    (*env)->DeleteLocalRef(env, service);
6464
ab55284b389f Fixed bug 1573 - SDL does not support system clipboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6448
diff changeset
   977
7501