src/core/android/SDL_android.c
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 29 Nov 2013 10:06:08 -0300
changeset 8039 3b0346b37e0f
parent 8029 cf81e6709b3d
child 8047 a5270cef21a7
permissions -rw-r--r--
Improve Android pause/resume behavior.
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"
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
    36
#include "../../video/android/SDL_androidwindow.h"
7927
4d984f2ea5bb Fixed implicit function declarations by including the missing header.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7926
diff changeset
    37
#include "../../joystick/android/SDL_sysjoystick.h"
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    38
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
    39
#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
    40
#include <pthread.h>
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
    41
#include <sys/types.h>
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
    42
#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
    43
#define LOG_TAG "SDL_android"
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
    44
/* #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) */
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
    45
/* #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) */
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
    46
#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
    47
#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
    48
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
    49
/* Uncomment this to log messages entering and exiting methods in this file */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
    50
/* #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
    51
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
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
    53
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
    54
/*******************************************************************************
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
 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
    56
*******************************************************************************/
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
#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
    58
#include <android/log.h>
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
    59
#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
    60
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
                               Globals
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
*******************************************************************************/
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    65
static pthread_key_t mThreadKey;
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    66
static JavaVM* mJavaVM;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
    68
/* Main activity */
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
    69
static jclass mActivityClass;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
    71
/* method signatures */
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
    72
static jmethodID midGetNativeSurface;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    73
static jmethodID midFlipBuffers;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    74
static jmethodID midAudioInit;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    75
static jmethodID midAudioWriteShortBuffer;
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
    76
static jmethodID midAudioWriteByteBuffer;
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
    77
static jmethodID midAudioQuit;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
    79
/* Accelerometer data storage */
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
    80
static float fLastAccelerometer[3];
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
    81
static bool bHasNewData;
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
/*******************************************************************************
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
                 Functions called by JNI
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
*******************************************************************************/
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
    87
/* Library init */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
    88
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
    89
{
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    90
    JNIEnv *env;
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    91
    mJavaVM = vm;
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    92
    LOGI("JNI_OnLoad called");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
    93
    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
    94
        LOGE("Failed to get the environment using GetEnv()");
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    95
        return -1;
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
    96
    }
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    97
    /*
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    98
     * Create mThreadKey so we can keep track of the JNIEnv assigned to each thread
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
    99
     * Refer to http://developer.android.com/guide/practices/design/jni.html for the rationale behind this
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   100
     */
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   101
    if (pthread_key_create(&mThreadKey, Android_JNI_ThreadDestroyed)) {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   102
        __android_log_print(ANDROID_LOG_ERROR, "SDL", "Error initializing pthread key");
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   103
    }
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   104
    else {
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   105
        Android_JNI_SetupThread();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   106
    }
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   107
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
    return JNI_VERSION_1_4;
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   111
/* 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
   112
void SDL_Android_Init(JNIEnv* mEnv, jclass cls)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
{
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   116
    Android_JNI_SetupThread();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   117
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   118
    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
   119
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   120
    midGetNativeSurface = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   121
                                "getNativeSurface","()Landroid/view/Surface;");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   122
    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
   123
                                "flipBuffers","()V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   124
    midAudioInit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
7552
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   125
                                "audioInit", "(IZZI)I");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   126
    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
   127
                                "audioWriteShortBuffer", "([S)V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   128
    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
   129
                                "audioWriteByteBuffer", "([B)V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   130
    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
   131
                                "audioQuit", "()V");
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   133
    bHasNewData = false;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   134
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   135
    if(!midGetNativeSurface || !midFlipBuffers || !midAudioInit ||
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   136
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   137
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   138
    }
5996
102a9ec1ea13 Don't use a global JNIEnv across threads; it's not thread safe.
Ryan C. Gordon <icculus@icculus.org>
parents: 5994
diff changeset
   139
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
}
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   141
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   142
/* Resize */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   143
void Java_org_libsdl_app_SDLActivity_onNativeResize(
4998
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   144
                                    JNIEnv* env, jclass jcls,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   145
                                    jint width, jint height, jint format)
4964
6c645018741e Build the SDL library as a shared object on Android, so it will work correctly with SDL_image and SDL_ttf.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
{
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   147
    Android_SetScreenResolution(width, height, format);
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   148
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   149
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   150
// Paddown
8013
109299fe73ad [Android] Try to improve handling of DPAD|GAMEPAD + KEYBOARD devices
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7927
diff changeset
   151
int Java_org_libsdl_app_SDLActivity_onNativePadDown(
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   152
                                    JNIEnv* env, jclass jcls,
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   153
                                    jint padId, jint keycode)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   154
{
8013
109299fe73ad [Android] Try to improve handling of DPAD|GAMEPAD + KEYBOARD devices
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7927
diff changeset
   155
    return Android_OnPadDown(padId, keycode);
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   156
}
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   157
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   158
// Padup
8013
109299fe73ad [Android] Try to improve handling of DPAD|GAMEPAD + KEYBOARD devices
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7927
diff changeset
   159
int Java_org_libsdl_app_SDLActivity_onNativePadUp(
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   160
                                   JNIEnv* env, jclass jcls,
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   161
                                   jint padId, jint keycode)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   162
{
8013
109299fe73ad [Android] Try to improve handling of DPAD|GAMEPAD + KEYBOARD devices
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7927
diff changeset
   163
    return Android_OnPadUp(padId, keycode);
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   164
}
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   165
7926
2d2a85852f74 Corrected source comment.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7910
diff changeset
   166
/* Joy */
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   167
void Java_org_libsdl_app_SDLActivity_onNativeJoy(
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   168
                                    JNIEnv* env, jclass jcls,
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   169
                                    jint joyId, jint axis, jfloat value)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   170
{
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   171
    Android_OnJoy(joyId, axis, value);
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   172
}
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7863
diff changeset
   173
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   174
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   175
/* Surface Created */
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   176
void Java_org_libsdl_app_SDLActivity_onNativeSurfaceChanged(JNIEnv* env, jclass jcls)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   177
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   178
    SDL_WindowData *data;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   179
    SDL_VideoDevice *_this;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   180
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   181
    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   182
        return;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   183
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   184
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   185
    _this =  SDL_GetVideoDevice();
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   186
    data =  (SDL_WindowData *) Android_Window->driverdata;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   187
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   188
    /* If the surface has been previously destroyed by onNativeSurfaceDestroyed, recreate it here */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   189
    if (data->egl_surface == EGL_NO_SURFACE) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   190
        if(data->native_window) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   191
            ANativeWindow_release(data->native_window);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   192
        }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   193
        data->native_window = Android_JNI_GetNativeWindow();
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   194
        data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   195
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   196
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   197
    /* GL Context handling is done in the event loop because this function is run from the Java thread */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   198
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   199
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   200
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   201
/* Surface Destroyed */
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   202
void Java_org_libsdl_app_SDLActivity_onNativeSurfaceDestroyed(JNIEnv* env, jclass jcls)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   203
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   204
    /* We have to clear the current context and destroy the egl surface here
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   205
     * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   206
     * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   207
     */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   208
    SDL_WindowData *data;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   209
    SDL_VideoDevice *_this;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   210
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   211
    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   212
        return;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   213
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   214
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   215
    _this =  SDL_GetVideoDevice();
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   216
    data = (SDL_WindowData *) Android_Window->driverdata;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   217
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   218
    if (data->egl_surface != EGL_NO_SURFACE) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   219
        SDL_EGL_MakeCurrent(_this, NULL, NULL);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   220
        SDL_EGL_DestroySurface(_this, data->egl_surface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   221
        data->egl_surface = EGL_NO_SURFACE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   222
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   223
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   224
    /* GL Context handling is done in the event loop because this function is run from the Java thread */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   225
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   226
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   227
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   228
void Java_org_libsdl_app_SDLActivity_nativeFlipBuffers(JNIEnv* env, jclass jcls)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   229
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   230
    SDL_GL_SwapWindow(Android_Window);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   231
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   232
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   233
/* Keydown */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   234
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
   235
                                    JNIEnv* env, jclass jcls, jint keycode)
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   236
{
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
   237
    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
   238
}
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
   239
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   240
/* Keyup */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   241
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
   242
                                    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
   243
{
4980
d9fdff945ec9 A bit of cleanup in the Android driver
Sam Lantinga <slouken@libsdl.org>
parents: 4964
diff changeset
   244
    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
   245
}
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
   246
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   247
/* Keyboard Focus Lost */
7564
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   248
void Java_org_libsdl_app_SDLActivity_onNativeKeyboardFocusLost(
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   249
                                    JNIEnv* env, jclass jcls)
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   250
{
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   251
    /* Calling SDL_StopTextInput will take care of hiding the keyboard and cleaning up the DummyText widget */
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   252
    SDL_StopTextInput();
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   253
}
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   254
d2bc997ef5d6 Temporary fix for bug #1639 SDL does not message back closing of screen keyboard
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7552
diff changeset
   255
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   256
/* Touch */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   257
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
   258
                                    JNIEnv* env, jclass jcls,
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5860
diff changeset
   259
                                    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
   260
                                    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
   261
{
5982
f324bd81b52c Added support for multitouch on Android.
Ryan C. Gordon <icculus@icculus.org>
parents: 5860
diff changeset
   262
    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
   263
}
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
   264
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   265
/* Accelerometer */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   266
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
   267
                                    JNIEnv* env, jclass jcls,
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   268
                                    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
   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
    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
   271
    fLastAccelerometer[1] = y;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   272
    fLastAccelerometer[2] = z;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   273
    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
   274
}
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
   275
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   276
/* Low memory */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   277
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
   278
                                    JNIEnv* env, jclass cls)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   279
{
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   280
    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
   281
}
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   282
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   283
/* Quit */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   284
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
   285
                                    JNIEnv* env, jclass cls)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   286
{
7910
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   287
    /* Discard previous events. The user should have handled state storage
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   288
     * in SDL_APP_WILLENTERBACKGROUND. After nativeQuit() is called, no
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   289
     * events other than SDL_QUIT and SDL_APP_TERMINATING should fire */
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   290
    SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   291
    /* Inject a SDL_QUIT event */
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   292
    SDL_SendQuit();
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   293
    SDL_SendAppEvent(SDL_APP_TERMINATING);
7910
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   294
    /* Resume the event loop so that the app can catch SDL_QUIT which
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   295
     * should now be the top event in the event queue. */
261addaa47d0 [Android] Fixes Bug 2041 - can't get SDL_QUIT event...
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7907
diff changeset
   296
    if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   297
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   298
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   299
/* Pause */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   300
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
   301
                                    JNIEnv* env, jclass cls)
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   302
{
8039
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   303
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   304
    if (Android_Window) {
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   305
        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
   306
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
8039
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   307
        SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   308
        SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   309
    
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   310
        /* *After* sending the relevant events, signal the pause semaphore 
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   311
         * so the event loop knows to pause and (optionally) block itself */
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   312
        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
   313
    }
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   314
}
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   315
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   316
/* Resume */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   317
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
   318
                                    JNIEnv* env, jclass cls)
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   319
{
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   320
    __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
   321
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   322
    if (Android_Window) {
6330
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   323
        /* 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
   324
         * 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
   325
         * 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
   326
         */
0fa55ca2efdd Fixes #1422, restores GL context automatically under Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6308
diff changeset
   327
        if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
8039
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   328
        
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   329
        SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
3b0346b37e0f Improve Android pause/resume behavior.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8029
diff changeset
   330
        SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
6186
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   331
        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
   332
        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
   333
    }
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   334
}
e565ac981de6 Fixed bug 1293 - [Android] Support Pause/Resume
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   335
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   336
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
   337
                                    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
   338
                                    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
   339
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   340
    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
   341
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   342
    SDL_SendKeyboardText(utftext);
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   343
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   344
    (*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
   345
}
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   346
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   347
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
   348
                                    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
   349
                                    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
   350
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   351
    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
   352
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   353
    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
   354
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   355
    (*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
   356
}
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   357
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   358
f2c03c06d987 Fixed bug 1614 - SDL for Android does not implement TextInput API
Sam Lantinga <slouken@libsdl.org>
parents: 6464
diff changeset
   359
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
   360
/*******************************************************************************
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
   361
             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
   362
*******************************************************************************/
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   363
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   364
static int s_active = 0;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   365
struct LocalReferenceHolder
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   366
{
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   367
    JNIEnv *m_env;
6650
d36232135316 Added information on running valgrind on Android
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
   368
    const char *m_func;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   369
};
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   370
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   371
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
   372
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   373
    struct LocalReferenceHolder refholder;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   374
    refholder.m_env = NULL;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   375
    refholder.m_func = func;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   376
#ifdef DEBUG_JNI
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   377
    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
   378
#endif
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   379
    return refholder;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   380
}
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   381
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   382
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
   383
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   384
    const int capacity = 16;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   385
    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
   386
        SDL_SetError("Failed to allocate enough JVM local references");
7516
4a98853e8e4c Corrected return value to be of correct type.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7515
diff changeset
   387
        return SDL_FALSE;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   388
    }
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   389
    ++s_active;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   390
    refholder->m_env = env;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   391
    return SDL_TRUE;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   392
}
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   393
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   394
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
   395
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   396
#ifdef DEBUG_JNI
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   397
    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
   398
#endif
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   399
    if (refholder->m_env) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   400
        JNIEnv* env = refholder->m_env;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   401
        (*env)->PopLocalFrame(env, NULL);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   402
        --s_active;
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   403
    }
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   404
}
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   405
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   406
static SDL_bool LocalReferenceHolder_IsActive()
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   407
{
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   408
    return s_active > 0;    
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   409
}
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   410
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   411
ANativeWindow* Android_JNI_GetNativeWindow(void)
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
   412
{
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   413
    ANativeWindow* anw;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   414
    jobject s;
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
   415
    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
   416
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   417
    s = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetNativeSurface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   418
    anw = ANativeWindow_fromSurface(env, s);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   419
    (*env)->DeleteLocalRef(env, s);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   420
  
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7612
diff changeset
   421
    return anw;
7567
52da75545aaa Fixes bug #1889, allows for GL Context deletion/recreation on Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7564
diff changeset
   422
}
52da75545aaa Fixes bug #1889, allows for GL Context deletion/recreation on Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7564
diff changeset
   423
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   424
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
   425
{
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   426
    JNIEnv *mEnv = Android_JNI_GetEnv();
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   427
    (*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
   428
}
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   429
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   430
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
   431
{
a514bfe6952a The window is changed to reflect the actual screen dimensions, for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4997
diff changeset
   432
    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
   433
    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
   434
    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
   435
    if (mid) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   436
        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
   437
        (*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
   438
        (*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
   439
    }
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
   440
}
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
   441
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   442
SDL_bool Android_JNI_GetAccelerometerValues(float values[3])
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   443
{
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   444
    int i;
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   445
    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
   446
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   447
    if (bHasNewData) {
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   448
        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
   449
            values[i] = fLastAccelerometer[i];
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   450
        }
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   451
        bHasNewData = false;
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   452
        retval = SDL_TRUE;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   453
    }
6212
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   454
78d854de3a66 Fixed bug 1368 - Enabling joystick subsystem cause an infinite loop
Sam Lantinga <slouken@libsdl.org>
parents: 6191
diff changeset
   455
    return retval;
5000
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   456
}
6a10693e66c3 Cleaned up internal accelerometer interface
Sam Lantinga <slouken@libsdl.org>
parents: 4998
diff changeset
   457
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
   458
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
   459
    /* 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
   460
    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
   461
    if (env != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   462
        (*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
   463
        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
   464
    }
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   465
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   466
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   467
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
   468
    /* 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
   469
     * 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
   470
     * 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
   471
     * 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
   472
     * 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
   473
     * 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
   474
     * 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
   475
     * 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
   476
     * 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
   477
     * 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
   478
     */
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   479
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   480
    JNIEnv *env;
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   481
    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
   482
    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
   483
        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
   484
        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
   485
    }
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   486
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   487
    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
   488
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   489
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   490
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
   491
    /* 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
   492
     * 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
   493
     * 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
   494
     * 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
   495
     * 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
   496
     * 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
   497
     * 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
   498
     *       (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
   499
     */
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   500
    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
   501
    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
   502
    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
   503
}
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   504
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   505
/*
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   506
 * Audio support
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   507
 */
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   508
static jboolean audioBuffer16Bit = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   509
static jboolean audioBufferStereo = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   510
static jobject audioBuffer = NULL;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   511
static void* audioBufferPinned = NULL;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   512
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   513
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
   514
{
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   515
    int audioBufferFrames;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   516
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
   517
    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
   518
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6335
diff changeset
   519
    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
   520
        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
   521
    }
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
   522
    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
   523
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   524
    __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
   525
    audioBuffer16Bit = is16Bit;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   526
    audioBufferStereo = channelCount > 1;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   527
7552
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   528
    if ((*env)->CallStaticIntMethod(env, mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames) != 0) {
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   529
        /* Error during audio initialization */
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   530
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioTrack initialization!");
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   531
        return 0;
463f78515a07 Fix for bug 2001, verify that the AudioTrack is properly initialized
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7516
diff changeset
   532
    }
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   533
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   534
    /* 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
   535
     * 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
   536
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   537
    if (is16Bit) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   538
        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
   539
        if (audioBufferLocal) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   540
            audioBuffer = (*env)->NewGlobalRef(env, audioBufferLocal);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   541
            (*env)->DeleteLocalRef(env, audioBufferLocal);
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   542
        }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   543
    }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   544
    else {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   545
        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
   546
        if (audioBufferLocal) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   547
            audioBuffer = (*env)->NewGlobalRef(env, audioBufferLocal);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   548
            (*env)->DeleteLocalRef(env, audioBufferLocal);
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   549
        }
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   550
    }
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   551
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   552
    if (audioBuffer == NULL) {
6802
8a6b8ce97656 Fix Audio Buffer allocation on Android >= 4.2
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6792
diff changeset
   553
        __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
   554
        return 0;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   555
    }
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   556
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   557
    jboolean isCopy = JNI_FALSE;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   558
    if (audioBuffer16Bit) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   559
        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
   560
        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
   561
    } else {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   562
        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
   563
        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
   564
    }
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   565
    if (audioBufferStereo) {
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   566
        audioBufferFrames /= 2;
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   567
    }
6864
97187387ad79 Bug 1703 - SDL calls a wrong JNI function on Android by Philipp Wiesemann
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6828
diff changeset
   568
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   569
    return audioBufferFrames;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   570
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   571
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   572
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
   573
{
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   574
    return audioBufferPinned;
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   575
}
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
   576
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   577
void Android_JNI_WriteAudioBuffer()
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   578
{
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
   579
    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
   580
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   581
    if (audioBuffer16Bit) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   582
        (*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
   583
        (*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
   584
    } else {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   585
        (*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
   586
        (*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
   587
    }
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
   588
4996
8d7315668e35 Fixed audio buffer lifecycle and implemented audio shutdown
Sam Lantinga <slouken@libsdl.org>
parents: 4995
diff changeset
   589
    /* 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
   590
}
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   591
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   592
void Android_JNI_CloseAudioDevice()
4995
9f9bea41e88f Working audio implementation contributed by Joseph Lunderville
Sam Lantinga <slouken@libsdl.org>
parents: 4989
diff changeset
   593
{
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
   594
    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
   595
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   596
    (*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
   597
4997
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   598
    if (audioBuffer) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   599
        (*env)->DeleteGlobalRef(env, audioBuffer);
4997
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   600
        audioBuffer = NULL;
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   601
        audioBufferPinned = NULL;
a21501393bef Removed code from GetPrimtiveArrayCritical() experiment
Sam Lantinga <slouken@libsdl.org>
parents: 4996
diff changeset
   602
    }
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
   603
}
4981
55b82067815b Fill in the video mode with the correct screen format
Sam Lantinga <slouken@libsdl.org>
parents: 4980
diff changeset
   604
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   605
/* Test for an exception and call SDL_SetError with its detail if one occurs */
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   606
/* If the parameter silent is truthy then SDL_SetError() will not be called. */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   607
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
   608
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   609
    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
   610
    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
   611
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   612
    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
   613
    if (exception != NULL) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   614
        jmethodID mid;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   615
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   616
        /* 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
   617
        (*mEnv)->ExceptionClear(mEnv);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   618
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
   619
        if (!silent) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   620
            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
   621
            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
   622
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   623
            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
   624
            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
   625
            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
   626
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   627
            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
   628
            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
   629
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
   630
            if (exceptionMessage != NULL) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   631
                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
   632
                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
   633
                (*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
   634
            } 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
   635
                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
   636
            }
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
   637
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   638
            (*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
   639
        }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   640
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   641
        return true;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   642
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   643
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   644
    return false;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   645
}
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   646
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   647
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
   648
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   649
    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
   650
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   651
    int result = 0;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   652
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   653
    jmethodID mid;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   654
    jobject context;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   655
    jobject assetManager;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   656
    jobject inputStream;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   657
    jclass channels;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   658
    jobject readableByteChannel;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   659
    jstring fileNameJString;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   660
    jobject fd;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   661
    jclass fdCls;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   662
    jfieldID descriptor;
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   663
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
   664
    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
   665
    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
   666
        goto failure;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   667
    }
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   668
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
   669
    fileNameJString = (jstring)ctx->hidden.androidio.fileNameRef;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   670
    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
   671
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   672
    /* context = SDLActivity.getContext(); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   673
    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
   674
            "getContext","()Landroid/content/Context;");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   675
    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
   676
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   677
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   678
    /* assetManager = context.getAssets(); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   679
    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
   680
            "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
   681
    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
   682
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   683
    /* 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
   684
    * 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
   685
    */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   686
    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
   687
    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
   688
    if (Android_JNI_ExceptionOccurred(true)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   689
        goto fallback;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   690
    }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   691
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   692
    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
   693
    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
   694
    if (Android_JNI_ExceptionOccurred(true)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   695
        goto fallback;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   696
    }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   697
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   698
    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
   699
    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
   700
    if (Android_JNI_ExceptionOccurred(true)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   701
        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
   702
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   703
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   704
    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
   705
    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
   706
    fdCls = (*mEnv)->GetObjectClass(mEnv, fd);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   707
    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
   708
    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
   709
    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
   710
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   711
    /* Seek to the correct offset in the file. */
6816
b3d3ef1e15b5 Fixes bug 1696, thanks Paul Geerts!
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6806
diff changeset
   712
    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
   713
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   714
    if (false) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   715
fallback:
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   716
        /* Disabled log message because of spam on the Nexus 7 */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   717
        /* __android_log_print(ANDROID_LOG_DEBUG, "SDL", "Falling back to legacy InputStream method for opening file"); */
7190
11612d544fcd Added mobile application events, with implementations for iOS and Android
Sam Lantinga <slouken@libsdl.org>
parents: 7149
diff changeset
   718
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   719
        /* Try the old method using InputStream */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   720
        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
   721
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   722
        /* inputStream = assetManager.open(<filename>); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   723
        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
   724
                "open", "(Ljava/lang/String;I)Ljava/io/InputStream;");
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   725
        inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString, 1 /* ACCESS_RANDOM */);
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   726
        if (Android_JNI_ExceptionOccurred(false)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   727
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   728
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   729
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   730
        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
   731
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   732
        /* Despite all the visible documentation on [Asset]InputStream claiming
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   733
         * that the .available() method is not guaranteed to return the entire file
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   734
         * size, comments in <sdk>/samples/<ver>/ApiDemos/src/com/example/ ...
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   735
         * android/apis/content/ReadAsset.java imply that Android's
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   736
         * AssetInputStream.available() /will/ always return the total file size
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   737
        */
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   738
        
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   739
        /* size = inputStream.available(); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   740
        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
   741
                "available", "()I");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   742
        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
   743
        if (Android_JNI_ExceptionOccurred(false)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   744
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   745
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   746
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   747
        /* readableByteChannel = Channels.newChannel(inputStream); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   748
        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
   749
        mid = (*mEnv)->GetStaticMethodID(mEnv, channels,
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   750
                "newChannel",
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   751
                "(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
   752
        readableByteChannel = (*mEnv)->CallStaticObjectMethod(
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   753
                mEnv, channels, mid, inputStream);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   754
        if (Android_JNI_ExceptionOccurred(false)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   755
            goto failure;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   756
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   757
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   758
        ctx->hidden.androidio.readableByteChannelRef =
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   759
            (*mEnv)->NewGlobalRef(mEnv, readableByteChannel);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   760
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   761
        /* 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
   762
        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
   763
                "read", "(Ljava/nio/ByteBuffer;)I");
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   764
        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
   765
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   766
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   767
    if (false) {
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   768
failure:
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   769
        result = -1;
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   770
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   771
        (*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
   772
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   773
        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
   774
            (*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
   775
        }
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
   776
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   777
        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
   778
            (*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
   779
        }
263e2c049c22 Fixes issue #1500 "SDL_RWops fails under Android 4" by removing stale Local Refs
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6307
diff changeset
   780
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   781
        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
   782
            (*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
   783
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   784
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   785
    }
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   786
    
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   787
    LocalReferenceHolder_Cleanup(&refs);
5650
640c67302f8e * Fix many memory leaks in Android FS code
Tim Angus <tim@blackcompanystudios.co.uk>
parents: 5582
diff changeset
   788
    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
   789
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   790
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   791
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
   792
        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
   793
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   794
    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
   795
    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
   796
    int retval;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   797
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   798
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   799
        LocalReferenceHolder_Cleanup(&refs);        
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   800
        return -1;
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   801
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   802
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
    if (!ctx) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   804
        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
   805
        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
   806
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   807
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   808
    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
   809
    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
   810
    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
   811
    ctx->hidden.androidio.readableByteChannelRef = NULL;
fbb84f5b985f Fixes #1519, adds initialization to Android RWops internal variables.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6330
diff changeset
   812
    ctx->hidden.androidio.readMethod = NULL;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   813
    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
   814
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   815
    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
   816
    LocalReferenceHolder_Cleanup(&refs);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   817
    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
   818
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   819
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   820
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
   821
        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
   822
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   823
    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
   824
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   825
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   826
        size_t bytesMax = size * maxnum;
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   827
        if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && ctx->hidden.androidio.position + bytesMax > ctx->hidden.androidio.size) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   828
            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
   829
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   830
        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
   831
        if (result > 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   832
            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
   833
            LocalReferenceHolder_Cleanup(&refs);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   834
            return result / size;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   835
        }
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   836
        LocalReferenceHolder_Cleanup(&refs);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   837
        return 0;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   838
    } else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   839
        jlong bytesRemaining = (jlong) (size * maxnum);
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   840
        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
   841
        int bytesRead = 0;
6284
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   842
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   843
        /* 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
   844
        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
   845
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   846
        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
   847
        if (!LocalReferenceHolder_Init(&refs, mEnv)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   848
            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
   849
            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
   850
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   851
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   852
        jobject readableByteChannel = (jobject)ctx->hidden.androidio.readableByteChannelRef;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   853
        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
   854
        jobject byteBuffer = (*mEnv)->NewDirectByteBuffer(mEnv, buffer, bytesRemaining);
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   855
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   856
        while (bytesRemaining > 0) {
7663
53fe1b64eb2d More non C89 compliant comments
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   857
            /* result = readableByteChannel.read(...); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   858
            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
   859
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   860
            if (Android_JNI_ExceptionOccurred(false)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   861
                LocalReferenceHolder_Cleanup(&refs);            
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   862
                return 0;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   863
            }
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
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   865
            if (result < 0) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   866
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   867
            }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   868
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   869
            bytesRemaining -= result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   870
            bytesRead += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   871
            ctx->hidden.androidio.position += result;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   872
        }
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   873
        LocalReferenceHolder_Cleanup(&refs);                    
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   874
        return bytesRead / size;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7190
diff changeset
   875
    }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   876
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   877
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   878
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
   879
        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
   880
{
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   881
    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
   882
    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
   883
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   884
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   885
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
   886
{
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   887
    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
   888
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   889
    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
   890
    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
   891
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   892
    if (!LocalReferenceHolder_Init(&refs, mEnv)) {
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   893
        LocalReferenceHolder_Cleanup(&refs);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   894
        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
   895
    }
1893d507ba42 Fixed bug 1417 - Android_JNI_FileClose local reference bug
Sam Lantinga <slouken@libsdl.org>
parents: 6212
diff changeset
   896
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   897
    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
   898
        if (release) {
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   899
            (*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
   900
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   901
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   902
        if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   903
            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
   904
            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
   905
                    "close", "()V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   906
            (*mEnv)->CallVoidMethod(mEnv, inputStream, mid);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   907
            (*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
   908
            if (Android_JNI_ExceptionOccurred(false)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   909
                result = -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   910
            }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   911
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   912
        else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   913
            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
   914
7677
871d43c6968a OCD fixes: Adds a space before */
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7663
diff changeset
   915
            /* inputStream.close(); */
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   916
            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
   917
                    "close", "()V");
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   918
            (*mEnv)->CallVoidMethod(mEnv, inputStream, mid);
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   919
            (*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
   920
            (*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
   921
            if (Android_JNI_ExceptionOccurred(false)) {
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   922
                result = -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   923
            }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   924
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   925
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   926
        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
   927
            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
   928
        }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   929
    }
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   930
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   931
    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
   932
    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
   933
}
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   934
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   935
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   936
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
   937
{
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   938
    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
   939
}
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6630
diff changeset
   940
7501
b27825bb5879 Android: Removed all unnecessary dependencies on C++.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 7368
diff changeset
   941
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
   942
{
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   943
    if (ctx->hidden.androidio.assetFileDescriptorRef) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   944
        switch (whence) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   945
            case RW_SEEK_SET:
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   946
                if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && offset > ctx->hidden.androidio.size) offset = ctx->hidden.androidio.size;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   947
                offset += ctx->hidden.androidio.offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   948
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   949
            case RW_SEEK_CUR:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   950
                offset += ctx->hidden.androidio.position;
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   951
                if (ctx->hidden.androidio.size != -1 /* UNKNOWN_LENGTH */ && offset > ctx->hidden.androidio.size) offset = ctx->hidden.androidio.size;
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   952
                offset += ctx->hidden.androidio.offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   953
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   954
            case RW_SEEK_END:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   955
                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
   956
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   957
            default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   958
                return SDL_SetError("Unknown value for 'whence'");
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   959
        }
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   960
        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
   961
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   962
        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
   963
        if (ret == -1) return -1;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   964
        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
   965
    } else {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   966
        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
   967
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   968
        switch (whence) {
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   969
            case RW_SEEK_SET:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   970
                newPosition = offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   971
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   972
            case RW_SEEK_CUR:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   973
                newPosition = ctx->hidden.androidio.position + offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   974
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   975
            case RW_SEEK_END:
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   976
                newPosition = ctx->hidden.androidio.size + offset;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   977
                break;
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   978
            default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7018
diff changeset
   979
                return SDL_SetError("Unknown value for 'whence'");
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   980
        }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   981
6806
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6802
diff changeset
   982
        /* Validate the new position */
9e57ff36fd7a Android: Access APK files using AssetFileDescriptor
Gabriel Jacobo <gabomdq@gmail.com>
parents: